PHP文件上传无法正常工作 - 无法移动

时间:2016-02-07 02:24:30

标签: php file-upload

HTML

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type='file' name='uphoto' id='uphoto' accept='image/x-png, image/gif, image/jpeg, image/jpg' >
<input type="submit" name="submit" value="Submit Changes">

PHP(profile.php)

    $target_dir = '/images/demo/profiles/';
    $target_file = $target_dir . basename($_FILES['uphoto']['name']);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


    if (!empty($_FILES['uphoto']['name'])) {
        //echo 'A file was selected for upload';

        // check file size
        if ($_FILES['uphoto']['size'] > 5000000) {
            echo '<p style="color:red">Sorry, the photo must be smaller than 5MB.</p>';
            $uploadOk = 0;
        }

        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
            echo '<p style="color:red">Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>';
            $uploadOk = 0;
        }

        if ($uploadOk == 0) {
            echo '<p style="color:red">Sorry, your file was not uploaded.</p>';
            // if everything is ok, try to upload file
        } else {
            if (move_uploaded_file($_FILES['uphoto']['tmp_name'], $target_file)) {
                echo '<p style="color:green">The file '. basename( $_FILES['uphoto']['name']). ' has been uploaded.</p>';
            } else {
                echo '<p style="color:red">Sorry, there was an error uploading your file:'.var_dump($_FILES['uphoto']['error']).'</p>';
            }
        }
    }

ERROR VAR DUMP

int(0)

错误日志

  

PHP警告:move_uploaded_file(/ images / demo / profiles / Blue hills.jpg):   无法打开流:没有这样的文件或目录   第182行/hermes/bosoraweb103/b1029/ipg.domain/sd/profile.php   PHP警告:move_uploaded_file():无法移动&#39; / tmp / phpLeRQSS&#39;至   &#39; / images / demo / profiles / Blue hills.jpg&#39;在   /hermes/bosoraweb103/b1029/ipg.domain/sd/profile.ph

表单实际上更复杂,除文件上传外,所有表单都有效。我只是修剪它以使其易于阅读。我联系了关于这个问题的婚礼主持人(iPage),但他们说他们的服务器工作正常,文件限制很大。

1 个答案:

答案 0 :(得分:0)

 $target_dir = './images/demo/profiles/';

而不是

 $target_dir = '/images/demo/profiles/';