【问题标题】:PHP upload images failurePHP上传图片失败
【发布时间】:2014-07-22 13:18:54
【问题描述】:

我正在使用 PHP 为 FileMaker 数据库创建一个网站。我要做的是上传一个图像文件,以便数据库可以访问它。

这是我的代码(来自教程):

if (isset($_POST['action']) and $_POST['action'] == 'Upload') {

    # set the destination directory for the image
    $image_directory = 'http://mydomain.com/Images/My_Example/';

    # set the temp and dest file names with paths
    $temporary_file = $_FILES['new_image']['tmp_name'];
    $destination_file = $image_directory . $_FILES['new_image']['name'];

    # move file to the images directory
    $result = move_uploaded_file($temporary_file, $destination_file);

    if ($result) {

        # insert the URL into the product record
        $edit = $fm->newEditCommand('Product', $_REQUEST['recid']);
        $edit->setField('Thumbnail URL', $destination_file);
        $edit->execute();

    } else {
        $error_message = nl2br("temporary_file = ".$temporary_file."\n");    
        $error_message.= nl2br("destination_file = ".$destination_file."\n\n");    
        echo $error_message;
        die('There was an error moving the file.');
    }
}

我知道函数“move_uploaded_file()”会移动临时文件,但我看不到实际上首先上传到临时文件的语句。请问我错过了什么? (不知道这算不算红鲱鱼!)

但是“move_uploaded_file()”失败了,我收到了错误消息(我添加了它以尝试调试它)和“死”消息。有什么方法可以得到错误代码,告诉我为什么它失败了?

我确信相关目录确实具有读/写权限。而 'mydomain.com' 实际上在脚本中被替换为真实的域。

感谢您的任何建议。我是PHP的新手,所以在回答时请记住这一点!非常感谢。

干杯

【问题讨论】:

  • 您要上传的文件夹的所有者是谁?
  • 如果这就是你的全部,我认为你是对的,实际上保存临时文件的代码丢失了。
  • 您不必提供完整路径。您的上传路径应从网络根目录开始
  • 该文件夹是否可以通过网络访问

标签: php image upload image-uploading


【解决方案1】:

http://mydomain.com/Images/My_Example/ 无效,因为它是 URL 路径。你需要指定一个目录路径:

//$image_directory = 'http://mydomain.com/Images/My_Example/';
// change to:
$image_directory = 'path/to/Images/folder/'; 

【讨论】:

  • 谢谢!(还有 8 个,它说,我想知道这意味着什么?)
【解决方案2】:

首先:试试这些错误信息

错误信息在:$_FILES['upfile']['error']

    switch ($_FILES['upfile']['error']) {
    case UPLOAD_ERR_OK:
        break;
    case UPLOAD_ERR_NO_FILE:
        throw new RuntimeException('No file sent.');
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        throw new RuntimeException('Exceeded filesize limit.');
    default:
        throw new RuntimeException('Unknown errors.');
}

其次:http://mydomain.com/Images/My_Example/ 不是有效的上传路径。

【讨论】:

  • 谢谢。首先,我一直在尝试获取 $_FILES 数组的内容列表,但找不到用于爱情或金钱的内容!谢谢那些!其次,如果你能说出它为什么无效,那将会很有用。 :)
【解决方案3】:

问题出在这句话中:

$image_directory = 'http://mydomain.com/Images/My_Example/';

当您上传文件时,您不能在目标路径中提供 url。您需要使用相对路径,例如:

$image_directory = './Images/My_Example/';

【讨论】:

  • 谢谢,这是解决问题的完美答案! :)
【解决方案4】:

您正在使用带有“http://mydomain.com/Images/My_Example/”的目标目录路径,这将不起作用。 为了保存文件,你需要在服务器上给它绝对路径——这意味着如果 DOCUMENT_ROOT 文件夹是“/var/www/html/”,那么你需要将它上传到“/var/www/html /图像/My_Example'。 您可以在 $_SERVER['DOCUMENT_ROOT'] 变量中检查 DOCUMENT_ROOT 路径,使用 php 中的 phpinfo() 命令或在 apache.conf 文件中。 您还需要确保您的 php 对您尝试保存的文件夹具有写入权限。

【讨论】:

  • 谢谢。我还不确定 DOCUMENT_ROOT 是什么,或者它在哪里!是的,它肯定有写权限,所以有人告诉我。我不是在自己的计算机上执行此操作,而是使用 FileMaker/PHP 主机。我认为我无法访问您提到的内容。我在 Cyber​​duck 中看到的是浏览器中域名之后的所有内容。以上所有我无法访问的内容。
猜你喜欢
  • 2014-10-25
  • 1970-01-01
  • 2015-04-12
  • 2012-01-13
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多