问题描述
我正在尝试使用PHP将iPhone上载的图像保存到服务器.我正在使用ASIHTTPRequest处理数据请求.这是我的iPhone代码:
I am trying to save an uploaded image from an iPhone to a server using PHP. I am using ASIHTTPRequest to handle the data request. This is my iPhone code:
[request setPostValue:someString forKey:@"string1"];
[request setPostValue:anotherString forKey:@"string2"];
[request addData:[NSData dataWithData:UIImageJPEGRepresentation(anImage, 0.9)]
withFileName:@"img.jpg" andContentType:@"image/jpeg" forKey:@"img"];
[request startSynchronous];
在服务器端,我目前正在使用以下PHP代码尝试将图像保存到服务器上的文件夹中:
On the server side, I am using this PHP code currently to try saving the image to a folder on my server:
print_r($_FILES);
$folder = 'upload/';
$image_path = basename($_FILES['img']['name']);
echo $folder . $image_path . "\n";
if (move_uploaded_file($_FILES['img']['tmp_name'], $folder . $image_path)) {
echo 'ok!';
}
else {
echo 'fail !';
}
字符串已上传得很好,并且当我print_r()
时,我按预期看到了图像数组的内容,但是由于某些原因,当试图保存图像时,我却收到了失败!"的消息.打印到控制台.怎么了?
The strings are uploaded just fine and when I print_r()
i see the contents of the image array as I expect, but for some reason when trying to save the image, i am getting "fail !" printed to the console. What is wrong here?
谢谢.
推荐答案
在移动图片之前尝试以下步骤
Try the following step before you move your image
if(!is_dir($folder))
{
echo "Folder does not exist" ;
if(!mkdir($folder))
{
echo "Attempt to create folder failed" ;
}
}
else if(!is_writable($folder))
{
echo "Folder nto writeable" ;
if(!chmod($folder, 0755))
{
echo "Attempt to make writeable failed" ;
}
}
else {
if (move_uploaded_file($_FILES['img']['tmp_name'], $folder . $image_path)) {
echo 'ok!';
}
else {
echo 'fail !';
}
}
您得到什么???
这篇关于PHP-将上传的图像保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!