图片上传表单给我“没有这样的文件或目录”错误

时间:2012-07-13 22:37:07

标签: php

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

2 个答案:

答案 0 :(得分:1)

您必须指定完整路径:

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

答案 1 :(得分: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目录chmod到777以允许将文件上传到该目录。