图片上传不能正常使用PHP?

时间:2016-03-27 16:49:08

标签: php file-upload

我正在尝试设置我的网站,以便我可以使用移动设备上传图片。但是,每当我尝试上传图像时,它都不起作用并抛出两个错误:

  

警告:move_uploaded_file(UPLOADS / Image Attachment 27/03 / 16.jpg)[function.move-uploaded-file]:无法打开流:/ home3 / hutch / public_html / stencylcollins / upload1中没有这样的文件或目录第29行的.php

     

警告:move_uploaded_file()[function.move-uploaded-file]:无法将'/ tmp / phpSZy8LW'移动到/ home3 / hutch / public_html / stencylcollins中的'UPLOADS / Image Attachment 27/03 / 16.jpg'第29行/upload1.php

在此之后我得到了我的回声

  

UPLOAD SUCCESSFUL:您的文档现已上传并准备发送。

任何人都知道这里发生了什么?

<?php
    //login 
    ob_clean();session_start();

    if (isset($_GET['logout'])){
    session_destroy();  
    }

    if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] == false) {
        header("Location: index.php");
    }

    //timezone 

    date_default_timezone_set('Europe/London');
    $date = date('d/m/y', time());

    //upload 
    if(isset($_FILES['UploadFileField'])){
        $allowed = array('jpg','png','jpeg');
        $name = $_FILES["UploadFileField"]["name"];
        $tmp = $_FILES['UploadFileField']['tmp_name'];
        $type = $_FILES['UploadFileField']['type'];
        $newName = "Image Attachment ".$date.".jpg";
        $types = array('jpg','png','jpeg');
        $ext = pathinfo($name, PATHINFO_EXTENSION);

        if(in_array($ext,$types)){
            move_uploaded_file($tmp, "UPLOADS/$newName");
            echo '<font color="#006600" size="3"><p align="center"><b>UPLOAD SUCCESSFUL: </font><font color="#000000" size="3">Your document has now been uploaded and is ready to send.</b></p></font>';
        } 

        else {
            if(!$tmp){
                echo '<font color="#FF0000" size="3"><p align="center"><b>UPLOAD FAILED: </font><font color="#000000" size="3">No document has been selected.</b></p></font>';
            }

            else {
                echo '<font color="#FF0000" size="3"><p align="center"><b>UPLOAD FAILED: </font><font color="#000000" size="3">Uploaded document was an incorrect extension type, please use ".jpg", ".jpeg", or "png" only.</b></p></font>';
            }
        }
    }
?>

2 个答案:

答案 0 :(得分:1)

您不能在文件名中包含斜杠。文件名Image Attachment 27/03/16.jpg完全无效,因为您实际上是指文件16.jpg中名为03的文件,该文件本身位于Image Attachment 27中。这些目录都不存在,因此您的错误。

答案 1 :(得分:-2)

带空格的文件名???我认为这是一种糟糕的方式。

mysqli_close($conn);

并且在你的代码中你不需要使用 // $newName = "Image Attachment ".$date.".jpg"; ??? $newName = "Image_Attachment_".$date.".jpg"; // you can check where you trying to upload a file if(is_dir("UPLOADS/")) { echo "dir ok";} else { echo "dir not ok"; } 进入日期函数,因为没有第二个参数的time()会返回当前日期:)