doujiu8178 2017-03-11 03:57
浏览 146
已采纳

linux服务器上的图片上传(PHP)

I am trying to upload image to server. The following code works when I use on local pc but when I try on server, image is not uploaded.

$img = $_FILES['file_uploaded']['name'];
$image_temp = $_FILES['file_uploaded']['tmp_name'];
if(move_uploaded_file($image_temp, "uploads/" . $img)){
    $fullpath =  dirname(__FILE__) . '\\images\\' . $img;
}
else
    echo 'Failed!';
  • 写回答

1条回答 默认 最新

  • dongluoheng3324 2017-03-11 04:07
    关注

    Do you have the same file permissions on server? Does folder /var/www/html/uploaded exist and is writable by web server user (usually www-data)?

    Try running following command in server terminal:

    sudo su
    mkdir /var/www/html/uploads
    chmod -R 775 /var/www/html/uploads
    chown -R www-data:www-data /var/www/html/uploads
    

    Where www-data is used, change it with your own web server user.

    "FIXED" SCRIPT:

    $img = $_FILES['file_uploaded']['name'];
    $upload_dir = "/var/www/html/uploads/";
    $image_temp = $_FILES['file_uploaded']['tmp_name'];
    if (file_exists($image_temp)) {
         echo "Temp file doesn't exist.";
         return false;
    }
    $move_file = move_uploaded_file($image_temp, $upload_dir . $img);
    if ($move_file) {
        $fullpath =  $upload_dir . $img;
    }
    else {
        echo 'Failed!';
        return false;
    }
    

    You should write additional checks, so you know where your application fails.

    Regarding security, you should never trust user. In this case we used filename ($_FILES['file_uploaded']['name'])provided by user, using which he could write and maybe even read outside of designated directory

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器