[英]PHP - Save image uploaded with AJAX
Given this one a few hours research and thought but I'm not getting anywhere. 鉴于此,我花了几个小时进行研究和思考,但是我什么也没做。
I have managed to get an image file to upload through AJAX using a FormData object. 我设法使用FormData对象获取了要通过AJAX上传的图像文件。 When the file reaches the php code I am able to access its info such as
'name'
and 'type'
by using: 当文件到达php代码时,我可以使用以下方式访问其信息,例如
'name'
和'type'
:
$_FILES['newFile']['name'];
$_FILES['newFile']['type'];
And so on, which means that the file must be uploading as intended, I just can't seem to save it to a file from there. 依此类推,这意味着该文件必须按预期进行上传,我似乎无法从那里将其保存到文件中。
I have tried: 我努力了:
$file = file_get_contents($_FILES["newFile"]['tmp_name']);
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
But then imagejpeg gives me the error "Expects paramater1 to be resource, string given." 但是,然后imagejpeg给我错误“期望paramater1是资源,给定字符串”。 So I tried:
所以我尝试了:
$file = imagecreatefromstring(file_get_contents($_FILES["newFile"]['tmp_name']));
imagejpeg($file, '/img/uploads/' . $_FILES["newFile"]['name']);
I now receive the error: /"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."
我现在收到错误:
/"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."
/ /"imagejpeg('/img/uploads/image.jpg'): Failed to open stream. No such file or directory."
Can someone explain how I get the image from the array to a file on disk? 有人可以解释我如何将图像从阵列中获取到磁盘上的文件吗?
您必须像这样使用move_uploaded_file函数:
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], $pathfilename)
使用php函数move_uploaded_file(“ $ _ FILES ['newFile'] ['tmp_name']”,“ Location_where_you_want_to_save”);
Figured it out! 弄清楚了! Obviously "/img/uploads/" was the issue.
显然“ / img / uploads /”是问题所在。 PHP doesn't recognize a forward slash as the root directory!
PHP无法将斜杠识别为根目录! My fault!
我的错!
move_uploaded_file($_FILES["newFile"]['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/img/uploads/' . $_FILES["newFile"]['name']);
Using $_SERVER['DOCUMENT_ROOT'] before the new file path worked perfectly. 在新文件路径完美运行之前,请使用$ _SERVER ['DOCUMENT_ROOT']。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.