为什么不是我的PHP移动文件脚本实际上移动文件? [英] Why isn't my php move file script actually moving the file?

查看:114
本文介绍了为什么不是我的PHP移动文件脚本实际上移动文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用php上传文件到服务器,而move_uploaded_file函数没有返回错误,文件不在目标文件夹中。正如你所看到的,我正在使用根目录的确切路径,并且正在上传的文件比最大尺寸要小。

  $ target =/ data / array1 / users / ultimate / public_html / Uploads / 2010 /; 
//将信息写入bioHold xml文件。
$ xml = new DOMDocument();
$ xml-> load('bioHold.xml');
$ xml-> formatOutput = true;
$ root = $ xml-> firstChild;
$ player = $ xml-> createElement(player);
$ image = $ xml-> createElement(image);
$ image-> setAttribute(loc,$ target.basename($ _ FILES ['image'] ['name']));
$ player-> appendChild($ image);
$ name = $ xml-> createElement(name,$ _POST ['name']);
$ player-> appendChild($ name);
$ number = $ xml-> createElement(number,$ _POST ['number']);
$ player-> appendChild($ number);
$ ghettoYear = $ xml-> createElement(ghettoYear,$ _POST ['ghetto']);
$ player-> appendChild($ ghettoYear);
$ schoolYear = $ xml-> createElement(schoolYear,$ _POST ['school']);
$ player-> appendChild($ schoolYear);
$ bio = $ xml-> createElement(bio,$ _POST ['bio']);
$ player-> appendChild($ bio);
$ root-> appendChild($ player);
$ xml-> save(bioHold.xml);
//将图像保存到服务器。
$ target = $ target.basename($ _ FILES ['image'] ['name']);
if(is_uploaded_file($ _ FILES ['image'] ['tmp_name']))
echo'这是一个文件< br />';
if(!(move_uploaded_file($ _ FILES ['image'] ['tmp_name'],$ target))){
echo $ _FILES ['image'] ['error']。< br />;
}
else {
echo $ _FILES ['image'] ['error']。< br />;
echo $ target;



$ b $ p
$ b $ p

。Eric R。

解决方案

大多数情况下,这是一个权限问题。我假定你没有任何直接的shell访问来直接检查这个东西,所以这里是如何从脚本中完成的:



检查如果存在 $ target 目录:

  $ target ='/ data /等等....'; 
if(!is_dir($ target)){
die(Directory $ target is not a directory);

$ / code>

检查它是否可写入:

  if(!is_writable($ target)){
die(Directory $ target is not writeable);

$ / code>

检查完整的目标文件名是否存在/可写 - 可能存在但可以不被覆盖:

  $ target = $ target。基名($ _ FILES [图像] [名称]); 
if(!is_writeable($ target)){
die(File $ target is not writeable);

code


$ p

$ b $($)$ $ $ $ $ $ $ $ $ $ $'$'$'$'$'$'$'$'$'$'$'$'错误']。< br />;

回应错误这里的参数是没用的,它纯粹是指上传过程。如果文件上传正确,但无法移动,则只会回显 0 (例如UPLOAD_ERR_OK常量)。检查错误的正确方法如下所示:

  if($ _FILES ['images'] ['error'] === UPLOAD_ERR_OK){
//如果(!is_uploaded_File(...)){
die(做了些什么 - 没有上传的文件),文件已经正确地上传了
;

if(!move_uploaded_file(...)){
echo无法移动文件,可能的诊断信息:
print_r(error_get_last());
die();

$ b} else {
die(Upload failed with error {$ _FILES ['images'] ['error']});
}


I am uploading files to a server using php and while the move_uploaded_file function returns no errors, the file is not in the destination folder. As you can see I am using the exact path from root, and the files being uploaded are lower than the max size.

$target = "/data/array1/users/ultimate/public_html/Uploads/2010/";  
//Write the info to the bioHold xml file.
$xml = new DOMDocument();
$xml->load('bioHold.xml');
$xml->formatOutput = true;
$root = $xml->firstChild;
$player = $xml->createElement("player");
$image = $xml->createElement("image");
$image->setAttribute("loc", $target.basename($_FILES['image']['name']));
$player->appendChild($image);
$name = $xml->createElement("name", $_POST['name']);
$player->appendChild($name);
$number = $xml->createElement("number", $_POST['number']);
$player->appendChild($number);
$ghettoYear = $xml->createElement("ghettoYear", $_POST['ghetto']);
$player->appendChild($ghettoYear);
$schoolYear = $xml->createElement("schoolYear", $_POST['school']);
$player->appendChild($schoolYear);
$bio = $xml->createElement("bio", $_POST['bio']);
$player->appendChild($bio);
$root->appendChild($player);
$xml->save("bioHold.xml");
//Save the image to the server.
$target = $target.basename($_FILES['image']['name']);
if(is_uploaded_file($_FILES['image']['tmp_name']))
    echo 'It is a file <br />';
if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}
else {
    echo $_FILES['image']['error']."<br />";
    echo $target;   
}

Any help is appreciated.

Eric R.

解决方案

Most like this is a permissions issue. I'm going to assume you don't have any kind of direct shell access to check this stuff directly, so here's how to do it from within the script:

Check if the $target directory exists:

$target = '/data/etc....';
if (!is_dir($target)) {
    die("Directory $target is not a directory");
}

Check if it's writeable:

if (!is_writable($target)) {
    die("Directory $target is not writeable");
}

Check if the full target filename exists/is writable - maybe it exists but can't be overwritten:

 $target = $target . basename($_FILES['image']['name']);
 if (!is_writeable($target)) {
     die("File $target isn't writeable");
 }

Beyond that:

if(!(move_uploaded_file($_FILES['image']['tmp_name'], $target))) {
    echo $_FILES['image']['error']."<br />";
}

Echoing out the error parameter here is of no use, it refers purely to the upload process. If the file was uploaded correctly, but could not be moved, this will still only echo out a 0 (e.g. the UPLOAD_ERR_OK constant). The proper way of checking for errors goes something like this:

if ($_FILES['images']['error'] === UPLOAD_ERR_OK) {
    // file was properly uploaded
    if (!is_uploaded_File(...)) {
        die("Something done goofed - not uploaded file");
    }
    if (!move_uploaded_file(...)) {
        echo "Couldn't move file, possible diagnostic information:"
        print_r(error_get_last());
        die();

    }
} else {
    die("Upload failed with error {$_FILES['images']['error']}");
}

这篇关于为什么不是我的PHP移动文件脚本实际上移动文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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