2012-10-28 175 views
0

我有這個功能將文件上傳到Web根目錄/連接/:上傳功能,如何獲得url

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

回答

0
+0

感謝,現在是很好的,但我有一個小問題,閃光會話無法顯示只是拳頭閃光會議顯示和其他人不!你認爲這個問題是什麼? –

+0

你想同時顯示多個消息嗎?這對CakePHP的基本組件來說是不可能的,需要在Session組件上工作。在網絡中搜索幾個例子。 總之,你需要創建一個setFlash方法來記錄多條消息。 –