move_upload_file,返回false,但仍可正常工作并且不能正确移动 [英] move_upload_file, return false but still working and not moving correctlly

查看:142
本文介绍了move_upload_file,返回false,但仍可正常工作并且不能正确移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将图像从android应用发送到服务器.问题是图像没有移动到正确的路径,而是仅在当前目录(仅在该php脚本存储的位置)移动.我在本地服务器和Web服务器上测试了此代码,得到了相同的结果.任何人都可以找出问题所在.

I am sending image from android apps to server. The problem is image not moving to the correct path, but only at current directory (only in which that php script stored). I tested this codes on local server and webserver, getting same result. Any one can find out whats problems.

本地服务器:XAMPP 1.7.7

Local Server: 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';
   ?>

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

Showing Output and Error in Local Server

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

Strict Standards: Resource ID#3 used as offset, casting to integer (3) in C:\xampp\htdocs\android\uploadSimage.php on line 12

注意:未定义的偏移量:第12行的C:\ xampp \ htdocs \ android \ uploadSimage.php中的3

Notice: Undefined offset: 3 in C:\xampp\htdocs\android\uploadSimage.php on line 12

false成功上传

在Web服务器中显示输出和错误

Showing Output and Error in Webserver

注意:未定义的偏移量:第12行的C:... \ uploadSimage.php中的3

Notice: Undefined offset: 3 in C:...\uploadSimage.php on line 12

false成功上传

推荐答案

move_uploaded_file()期望第二个参数是一个字符串,代表新的上传路径和文件名.当前,您仅通过一条路径.我也质疑路径是否正确.它必须是完整路径或相对路径.

move_uploaded_file() expects the second parameter to be a string representing the new path and filename of upload. Currently, you are passing only a path. I also question whether the path is correct. It must be a full path, or a relative path.

您还错误地使用了$_FILES数组.您是否通过在base64中进行编码并通过URL的查询字符串传递图像来上传图像?还是您实际上是使用multipart/form-data文件上传字段上传的?

You are also using the $_FILES array incorrectly. Are you uploading the image by encoding it in base64 and passing it via the URL's query string? Or are you actually uploading it using a multipart/form-data file upload field?

如果您上传的文件属于上传字段image,那么您将可以访问以下文件:

If you uploaded a file belonging to the upload field called image then you would get access to the file like this:

$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

然后您将像这样移动它:

You would then move it like this:

// 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中编码图像并通过URL发送图像来上传图像,则根本不需要move_uploaded_file;在这种情况下,您可以将解码后的内容写入任何您喜欢的文件中.请记住,URL的长度可能会受到限制,因此通过base64在URL中发送图像可能不是一个好主意.


If you are in fact uploading the image by encoding it in base64 and sending it over the URL, then you don't need move_uploaded_file at all; in that case you can just write the decoded contents to a file anywhere you like. Keep in mind, the length of the URL may be limited so sending the image in the URL via base64 may not be a good idea.


要在后续答案中对问题进行评论:php函数 move_uploaded_file()仅在您使用以下文件时使用使用HTTP POST方法上载将尝试移动的内容上载到PHP.它会进行内部检查,以查看您要移动的文件是否已上传到PHP.如果不是,则不会移动文件.因此,您不应使用move_uploaded_file(),因为您确认已通过URL上传图像.

EDIT 2:
To comment on the questions in your subsequent answer: The php function move_uploaded_file() should only be used when the file you are trying to move was uploaded to PHP using an HTTP POST method upload. It does an internal check to see if the file you are trying to move was uploaded to PHP. If it was not, then it won't move the file. Therefore you shouldn't be using move_uploaded_file() since you confirmed you were uploading the image through the URL.

由于您的PHP脚本的路径为C:\xampp\htdocs\android,因此这意味着根路径为C:\.服务器根目录与您的Web根目录或文档根目录不同,两者都相对于您的公共目录.任何时候使用PHP处理读/写文件时,都使用完整的服务器路径(相对于C:\/).

Since your PHP script's path is C:\xampp\htdocs\android, this means the root path is C:\. The server root is different from your web root or document root which are both relative to your public directory. Any time you are dealing with reading/writing files in PHP, you use the full server path (relative to C:\ or /).

鉴于新的事实,请尝试一些类似的代码来上传"图像:

Given the new facts, try some code like this to "upload" the image:

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

请务必采取必要的预防措施,以免我无法为其他用户上传图片.

Make sure to take the necessary precautions so I can't upload an image for another user.

这篇关于move_upload_file,返回false,但仍可正常工作并且不能正确移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆