move_uploaded_file()期望第二个参数是表示上传的新路径和文件名的字符串。目前,您只传递路径。我也质疑这条道路是否正确。它必须是完整路径或相对路径。您可能还错误地使用了$_FILES
数组。你是通过在base64中编码并通过URL的查询字符串传递图像来上传图像的吗?或者你真的使用multipart/form-data
文件上传字段上传它?
如果上传属于上传字段一个名为image
那么你就可以访问这样的文件:
$origname = $_FILES['image']['name']; // the name from the client device
$temppath = $_FILES['image']['tmp_name']; // the temp location on the PHP server
$error = $_FILES['image']['error']; // > 0 if there was an error
$size = $_FILES['image']['size']; // size of the file
$type = $_FILES['image']['type']; // mime type, cannot be trusted though
你会然后移动它是这样的:
// Be careful using the original file name.
// If the user uploads a file with a .php extension, they may be
// able to run PHP code on your server if they can access the upload folder
// You should either generate a random file name or remove the extension
// IF THE DESTINATION FILE EXISTS, IT WILL BE OVERWRITTEN
$newPath = '/home/yoursite/htdocs/uploads/' . $origname;
$moved = move_uploaded_file($_FILES['image']['tmp_name'], $newPath);
if ($moved) {
echo "File was moved successfully.";
} else {
echo "Failed to move file.";
}
编辑:
如果你实际上传图像,通过在base64中编码并通过UR发送L,那么根本就不需要move_uploaded_file
;在这种情况下,您可以将解码后的内容写入任何你喜欢的文件中。请记住,URL的长度可能有限,因此通过base64在URL中发送图像可能不是一个好主意。
编辑2:
要在随后的回答对问题发表评论:当你试图移动的文件使用HTTP POST方法上传上传到PHP PHP函数move_uploaded_file(),才应使用。它会进行内部检查,看看您尝试移动的文件是否已上传到PHP。如果不是,那么它不会移动文件。因此,您不应该使用move_uploaded_file()
,因为您确认您是通过URL上传图像的。
由于您的PHP脚本的路径是C:\xampp\htdocs\android
,这意味着根路径是C:\
。服务器根目录与Web根目录或文档根目录不同,它们都与公用目录相关。任何时候你正在用PHP读写文件,你都使用完整的服务器路径(相对于C:\
或/
)。
鉴于新的事实,尝试一些像这样的代码为“上传”的形象:
<?php
$base = (isset($_REQUEST['image'])) ? $_REQUEST['image'] : '';
$Username = (isset($_REQUEST['Username'])) ? trim($_REQUEST['Username']) : '';
$binary = @base64_decode($base);
if (empty($Username)) {
die('no username specified');
}
if (!$binary) {
// data was not in base64 or resulted in an empty string
die('invalid image uploaded');
}
$basePath = 'C:\\xampp\\htdocs\\android\\ProfileImage\\';
$imagePath = $basePath . $Username . '.png';
$file = @fopen($imagePath, 'w+');
if (!$file) {
die('failed to open ' . $imagePath . ' for writing');
}
fwrite($file, $binary);
fclose($file);
echo 'Successfully Uploaded';
确保采取必要的预防措施,所以我不能上传图片为其他用户。
感谢您的回复 – Manish 2012-02-21 18:47:08
太棒了!有效。非常感谢你。下次我会发布一些好问题 – Manish 2012-02-21 22:19:34
不客气。这是一个很好的问题,很高兴你能开始工作。快乐PHP'ing。 – drew010 2012-02-21 22:33:10