2012-02-20 91 views
1

我发送图像从android应用程序到服务器。问题是图像没有移动到正确的路径,但只在当前目录(只有在该PHP脚本存储)。我在本地服务器和网络服务器上测试了这些代码,得到相同的结果。任何人都可以找出最新的问题。move_upload_file,返回false,但仍然工作,不正确移动

本地服务器:XAMPP 1.7.7

我的PHP脚本:

<?php 
    $base=$_REQUEST['image']; 
    $Username=$_REQUEST['Username']; 
    $binary=base64_decode($base); 
    header('Content-Type: bitmap; charset=utf-8'); 

    $file = fopen($Username.'.png', 'w'); 
    fwrite($file, $binary); 

    $uploadFilename = '/htdocs/android/ProfileImage/'; 

    $tr =move_uploaded_file($_FILES[$file]['tmp_name'], $uploadFilename); 
    if($tr) 
    echo 'true'; 
    else 
    echo 'false'; 
echo 'Successfully Uploaded'; 
    ?> 

显示输出和错误在本地服务器

严格的标准:资源ID#3作为在第12行C:\ xampp \ htdocs \ android \ uploadSimage.php中将其转换为整数(3)

注意:未定义偏移量:3在C:\ XAMPP \ htdocs中\机器人\ uploadSimage.php第12行

falseSuccessfully上传

显示在网络服务器输出和错误

说明:未定义偏移:3在C:\ uploadSimage.php上线12

falseSuccessfully上传

回答

1

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'; 

确保采取必要的预防措施,所以我不能上传图片为其他用户。

+0

感谢您的回复 – Manish 2012-02-21 18:47:08

+0

太棒了!有效。非常感谢你。下次我会发布一些好问题 – Manish 2012-02-21 22:19:34

+0

不客气。这是一个很好的问题,很高兴你能开始工作。快乐PHP'ing。 – drew010 2012-02-21 22:33:10

相关问题