2012-07-13 41 views
0

PHP:圖片上傳表格給我 「沒有這樣的文件或目錄」 錯誤

if((isset($_POST['title']))&&(isset($_POST['body']))){ 

$maxSize = 4000; 
$acceptType = array( 
    'png' => 'image/png', 
    'jpe' => 'image/jpeg', 
    'jpeg' => 'image/jpeg', 
    'jpg' => 'image/jpeg', 
    'gif' => 'image/gif', 
); 

$title = mysql_real_escape_string($_POST['title']); 
$body = mysql_real_escape_string($_POST['body']); 


if((isset($_FILES['image']))&&(is_uploaded_file($_FILES["image"]["tmp_name"]))){ 

    $image_dest = "/it/images/".rand(1,1000)."-".basename($_FILES['image']['name']); 

    if($_FILES['image']['size'] <= $maxSize*1024) { 

     if(in_array($_FILES['image']['type'], $acceptType)) { 

      if(move_uploaded_file($_FILES['image']['tmp_name'], $image_dest)) // line 44 
       $message="Ok"; 
     } 
    } 



} 
} 

HTML:

<form action="createblog.php" method="post" enctype="multipart/form-data"> 
    <p> 
     <label>Title:</label> 
     <input type="text" size="100" name="title"> 
    </p> 
    <p> 
     <label>Image:</label> 
     <input type="file" name="image"> 
    </p> 
    <p> 
     <label>Image Caption:</label> 
     <input type="text" size="100" name="imagecaption"> 
    </p> 
    <p> 
     <label for="editor1">Body:</label> 
     <textarea class="ckeditor" cols="50" id="editor1" name="body" rows="10"></textarea> 
    </p> 
    <p> 
     <input type="submit" value="Submit" /> 
    </p> 
</form> 

爲什麼我得到這個錯誤?

Warning: move_uploaded_file(/it/images/865-Star_Trek_Logo.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/08/1936/html/it/createblog.php on line 44 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php0hvJSh' to '/it/images/865-Star_Trek_Logo.jpg' in /home/content/08/1936/html/it/createblog.php on line 44 
+1

什麼是不明確的錯誤信息? '沒有這樣的文件或目錄' – 2012-07-13 22:38:06

+0

但是什麼目錄? – xRobot 2012-07-13 22:39:00

+1

在服務器的根目錄中是否有一個名稱爲「it」的文件夾? – 32bitfloat 2012-07-13 22:42:22

回答

1

你必須指定完整路徑:

$image_dest = "/home/content/08/1936/html/it/images/".rand(1,1000)."-".basename($_FILES['image']['name']); 
+0

$ image_dest是文件名_with_的位置,因此它不是,也不需要是目錄。 – Austin 2012-07-13 22:46:46

+0

/images /是目錄,'rand(1,1000)。「 - 」。basename($ _ FILES ['image'] ['name']'是生成的文件名,而不是目錄 – Austin 2012-07-13 22:48:04

+0

Sry是我的錯。修正,謝謝 – Besnik 2012-07-13 22:49:31

1

您$ image_dest的開頭應爲:

/home/content/08/1936/html/it/images/ 

您正在嘗試使用相對路徑,而絕對路徑是必需的,所以:

$image_dest = "/home/content/08/1936/html/it/images/".rand(1,1000)."-".basename($_FILES['image']['name']); 

另請注意,您必須將您的images目錄修改爲777以允許將文件上載到該目錄。

相關問題