Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 移动时崩溃\u上传\u文件命令_Php_File - Fatal编程技术网

Php 移动时崩溃\u上传\u文件命令

Php 移动时崩溃\u上传\u文件命令,php,file,Php,File,我在执行move\u上传的\u文件命令时出错500。请注意,我没有返回false。不知道如何从中获取任何错误消息,但这显然会有所帮助 我从一个数组$\u文件开始,在我的测试中,它包含两个文件上的元数据,这是一个非常可控的测试。当我只上传一个文件时,这一行工作正常,所以我知道这不是服务器权限问题 <?php print_r($_FILES); $target_dir = "client_resources/"; foreach ($_FILES as $thisFile) {

我在执行move\u上传的\u文件命令时出错500。请注意,我没有返回false。不知道如何从中获取任何错误消息,但这显然会有所帮助

我从一个数组$\u文件开始,在我的测试中,它包含两个文件上的元数据,这是一个非常可控的测试。当我只上传一个文件时,这一行工作正常,所以我知道这不是服务器权限问题

<?php

print_r($_FILES);
$target_dir = "client_resources/";

foreach ($_FILES as $thisFile) {

    print_r($thisFile);

    $target_file = $target_dir . basename($thisFile['name']);

    echo $thisFile['tmp_name'];
    echo $target_file;

    if (move_uploaded_file($thisFile['tmp_name'], $target_file)) {
        echo 'uploaded';
    } else {
        // don't know how to get the error message from here
    }
}
?>
我也试着把这条线放在试抓块里,但我还是崩溃了

有人能理解为什么这不再有效吗?谢谢

像这样试试

$move = @ move_uploaded_file($thisFile['tmp_name'], $target_file);  
if(!$move)
    echo 'Not uploaded';
else
 echo 'uploaded';

使用此选项可以获取错误

$_FILES["pictures"]["error"];
如果您使用的是多个文件上传,那么使用像这样的数组上传功能

   //Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['upload']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

以下是错误的说明

在创建脚本时,应始终打开错误报告,以确保不会遇到任何问题。这可以通过以下方式实现:

ini_set('display_errors', 1);
error_reporting(E_ALL);
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/client_resources/';
现在,您面临的另一个问题是,您试图将文件存储在相对路径中。要移动文件,需要绝对路径。你可以像以前那样做:

ini_set('display_errors', 1);
error_reporting(E_ALL);
$target_dir = $_SERVER['DOCUMENT_ROOT'] . '/client_resources/';

或者研究一下。获取站点的当前工作目录

在代码中,您尚未检查文件上载大小!,默认文件上传大小是2MB。你检查过你的php.ini吗?在我实际的完整代码中,我检查过大小。正如我提到的,当我的脚本只是上传一个文件时,这个命令就起作用了。另外,您可以在输出中看到文件大小。我认为您正在尝试上载多个图像。右???打开错误报告-ini_设置“显示错误”,1;错误报告全部;请在脚本的顶部显示错误。我还保证猜测它失败的原因是因为您没有使用绝对文件路径$target\u file。@Darren-就是这样。我将$target\u dir行更改为$target\u dir=$\u SERVER['DOCUMENT\u ROOT']/客户资源/';它成功了。如果你提出这样的回答,我会接受的。非常感谢你!非常感谢。这样可以防止它崩溃。我的输出现在包括“未上传”毫不奇怪:P!我没有投你反对票,但我猜这是因为你的解决方案只会延迟问题被注意到。我已经知道上传失败了。这就是为什么我不能接受这个解决方案——它并不能真正解决任何问题。我的回答,是否回答了你的问题?你的问题是崩溃的权利,那么你还想让我做什么,为你写代码??我只是为你的问题回答了“移动时崩溃”上传文件命令这都是在应用了我的答案之后你没有崩溃,对吗?谁投了票,请说明原因,否则离开这个地方我没有投票,但这是一个糟糕的答案。永远不要用@抑制函数错误。改为修复错误。我不知道这将如何工作,因为我的程序在我有机会输出错误消息之前崩溃。调试输出已经表明上载部分本身没有错误。谢谢,我非常感谢!