dsozqcx9668 2017-12-25 11:23
浏览 22
已采纳

PHP:不上传图片

<?php

if(isset($_POST["submit-post"])){
    $desc = $_POST['description'];
    if (strpos($desc, "'") !== false) {
        $desc = str_replace("'", "_", $desc);
    }
    $sql_home = "INSERT INTO home (description) VALUES ('$desc');";
    if($conn->query($sql_home) === TRUE){ echo "<br>Great!<br>"; }
    else{ echo("<br>Nope!<br> "); }
    $uploaddir = $_SERVER['DOCUMENT_ROOT']. '/imgs/home/';
    $uploadfile = $uploaddir . basename($_FILES['image']['name']);
    if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded. <br>";
    } else {
        echo "Possible file upload attack! <br>";
        echo "$uploadfile <br>";
    }
    echo '<br>Here is some more debugging info: ';
    print_r($_FILES);
    echo "<br>";
    error_reporting(-1); ini_set("display_errors", true); //?
}

?>

Every time I try to upload an image ( .jpg ) it says "Possible file upload attack!". Help. Gives me two warnings:

Warning: move_uploaded_file(/var/www/html/imgs/home/calcetto.jpg): failed to open stream: Permission denied in /var/www/html/admin/db/manage.php on line 199
Warning: move_uploaded_file(): Unable to move '/tmp/phpGvLsAC' to '/var/www/html/imgs/home/calcetto.jpg' in /var/www/html/admin/db/manage.php on line 199

Please note:

  1. I tried both absolute and relative paths;
  2. php.ini: file_uploads = On
  3. I copied and pasted the code from php.net
  4. this file is /var/www/html/admin/db/manage.php
  5. images to upload are here: /var/www/html/imgs/home

Here is the html:

<form action="" method="post" enctype="multipart/form-data">
    <textarea name='description' placeholder='Image description'></textarea><br>
    <input type="file" name="image"><br><br>
    <input type='submit' name='submit-post' value='Upload'></input>
</form>
  • 写回答

1条回答 默认 最新

  • duannuan0074 2017-12-25 12:16
    关注

    I am writing my answer from comments.

    Please check for the errors by PHP error reporting

    just write

    error_reporting(-1);
    ini_set("display_errors", true);
    

    you need to check for permission to the upload directory, if you have subdirectory then you need to have write permission on both of the directories

    you can change the permission by the following commands -R will update the permissions for subdirectories as well

    chmod -R 777 uploaddirectory
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)