I am using ajax to upload a picture from the user's computer to my folder whenever he/she selects a photo from the file dialog?
Can move_uploaded_file()
work up here? I don't think so. :)
首先,如果您想使用 ajax 进行文件上传 - 使用dropzone非常容易做到这一点。它为您提供了许多功能,例如拖放/预览/删除以及更多功能
回到你的问题:
为什么你认为它不会?
你必须做这样的事情
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts)){
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}
}
} else {
echo "Invalid file";
}
?>
PS文件上传是一个非常棘手的问题。实现它很容易,但实现起来并不安全。为了以正确的方式做事 - 请考虑阅读此安全主题
https://security.stackexchange.com/questions/32852/risks-of-a-php-image-upload-form
https://security.stackexchange.com/questions/29719/secure-php-upload-form-storage?rq=1
if (move_uploaded_file($source, $target)) {
}
您可以简单地使用copy,如果它没有上传并且您可以访问客户端计算机...
使用示例:
$file = 'example.txt';
$newfile = 'example.txt.bak';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
或者像这样使用 xcopy:
exec('xcopy c:\\myfolder d:\\myfolder /e/i', $a, $a1);
假设您具有完全访问权限