dpjhq00684 2013-11-03 12:36
浏览 31

PHP move_uploaded_file

I am trying to upload files to a server using PHP move_uploaded_file and I am getting the following error:

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /Users/Rick/Sites/upload/upload.php on line 7

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpDlCZUd' to '/Users/Rick/Sites/upload/uploads/richardgregson' in /Users/Rick/Sites/upload/upload.php on line 7

Below is my code, nothing complicated.

if($_POST["upload"]){

$target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"];

    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)){
        echo "<div class='success'>The file " . "<span class='filename'>" . basename( $_FILES['uploadedfile']['name']) . "</span>" . " has been uploaded</div>";
    } else {
        echo "<div class='error'>There was an error uploading the file, please try again!</div>";
    }
}

The permissions on the folder it is writing to are correct. I dont understand the error "cannot be a directory as the argument is where we are moving the file to so it has to be a directory.."

Thanks

Rick

  • 写回答

1条回答 默认 最新

  • dongliang1893 2013-11-03 12:40
    关注

    The error is telling you what's wrong - $target_path (/Users/Rick/Sites/upload/uploads/richardgregson) is a directory

    Try adding an extension.

    $extension=end(explode('.', $_FILES["uploadedfile"]["name"]));
    $target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '.' . $extension;
    

    Or perhaps your putting the file into a folder

    $target_path = "/Users/Rick/Sites/upload/uploads/" . $_POST["name"] . '/' . $_FILES["uploadedfile"]["name"];
    

    Note: you should not trust the user to upload only the files you want and you should sanitise $_POST["name"], all too easy to make that '../../' and post a PHP file.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100