0

我有这个功能来上传文件到 webroot/attach/ :

   public function upload(){

    if ($this->request->is('post')) {
        if($_FILES["file"]["name"]){
    $allowedExts = array("torrent");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    if (($_FILES["file"]["size"] < 20000)&& in_array($extension, $allowedExts))
    {
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
    $this->Session->setFlash(__("Upload: " . $_FILES["file"]["name"] . "<br />" . 
     "Type: " . $_FILES["file"]["type"] . "<br />" . 
     "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"
    ));
    if (file_exists("../webroot/attach/torrents/" ."3alany_com_". $_FILES["file"]["name"]))
      {
    $this->redirect(array('action' => 'index'));
    $this->Session->setFlash(__($_FILES["file"]["name"] . " موجود قبل كده يا بوب "));   
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../webroot/attach/torrents/"."3alany_com_". $_FILES["file"]["name"]);

        $filename = "3alany_com_". $_FILES["file"]["name"];
        $filesize = $_FILES["file"]["size"];
        $user_id = $this->Session->read('Auth.User.id');
        mysql_set_charset('utf8');

    $this->Attachment->query("INSERT INTO attachments (
                filename ,  type , size , user_id
                ) VALUES (
                '{$filename}' , 'torrent', '{$filesize}' , '$user_id' 
                );");

    $this->redirect(array('action' => 'index'));
    $this->Session->setFlash(__(" موجود قبل كده يا بوب ")); 
      }
    }
    }else
      {
    $this->redirect(array('action' => 'index'));
    $this->Session->setFlash(__("أمتداد الفايل مش مقبول")); 
      }
    }


#############IMAGE

if ($_FILES["image"]["name"])
{
if (file_exists("../webroot/attach/images/" ."3alany_com_". $_FILES["image"]["name"]))
{
    $this->Session->setFlash(__($_FILES["image"]["name"] . " موجود قبل كده يا بوب "));  
}
else
{

$uploadedfile = $_FILES['image']['tmp_name'];
$file_name = stripslashes($_FILES['image']['name']);
$boss = explode(".", strtolower($file_name));
$extension = strtolower(end($boss)); 
$array = array('jpg', 'jpeg', 'png', 'gif'); 
if (!in_array($extension, $array))
{
    $this->redirect(array('action' => 'index'));
    $this->Session->setFlash(__("أمتداد الفايل مش مقبول")); 
}
$size = filesize($_FILES['image']['tmp_name']);
if ($size > 10000000)
{
echo "الصوره المضاعه كبيره";
exit;
}
if ($extension == "jpg" || $extension == "jpeg")
{
$src = imagecreatefromjpeg($uploadedfile);
} else
if ($extension == "png")
{
$src = imagecreatefrompng($uploadedfile);
} else
{
$src = imagecreatefromgif($uploadedfile);
}
list($width, $height) = getimagesize($uploadedfile); 
if ($width < 685)
{
$newwidth = $width;
} else
{
$newwidth = 500;
};
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

$filename = "../webroot/attach/images/". "3alany_com_" . $file_name;
imagejpeg($tmp, $filename, 100);
imagedestroy($src);
imagedestroy($tmp);

        mysql_set_charset('utf8');
    $filename = "3alany_com_". $_FILES["image"]["name"];
    $filesize = $_FILES["image"]["size"];
    $user_id = $this->Session->read('Auth.User.id');
    $this->Attachment->query("INSERT INTO attachments (
                filename ,  type , size , user_id
                ) VALUES (
                '{$filename}' , 'image', '{$filesize}' , '$user_id' 
                );");


$this->redirect(array('action' => 'index'));
$this->Session->setFlash(__(" موجود قبل كده يا بوب ")); 

      }

}




}
}

但我的问题是如何获得这些文件,我尝试了很多方法,比如:

html->url('/attach/torrents/' . $p['Attachment']['filename'], true); ?>

但它没有用,它给了我“错误:无法找到 AttachController。”

那我该如何处理这件事,谢谢:)!

ps:我有蛋糕2

4

1 回答 1

0

查看 cakephp 的全局常量:

http://book.cakephp.org/2.0/en/core-libraries/global-constants-and-functions.html

于 2012-10-28T09:27:31.923 回答