【问题标题】:PHP file upload/move doesn't workPHP文件上传/移动不起作用
【发布时间】:2015-12-01 09:03:42
【问题描述】:

我正在尝试使用以下代码将图像从 tmp 目录移动/复制/上传到另一个目录,但我没有看到我的图像被移动/复制到目标。有没有其他选择?

    public function uploadToS3($file, $tmp_url) {
    if (strrpos($file, '.tmp') == strlen($file)-4) {
                $file = substr($file, 0, strlen($file)-4);
            }
        $destination = "var/www/html/image" . $file;

    if (file_exists($destination)) {
        echo 'File '. $destination. ' already exists!';
    } else {
    move_uploaded_file($tmp_url, $destination);
    }
}
    //Ex: $tmp_url = http://localhost/mage/media/tmp/catalog/product/s/c/abc.png
    //Ex: $destination = /var/www/html/image/s/c/abc.png

在下面的 cmets 之后,我尝试了以下 php 代码,但也失败了

 <?php
$tmp_url = "var/www/html/abc/abc.png";
$destination = "var/www/html/des/abc.png";
 if(move_uploaded_file($tmp_url, $destination)){echo "success";}else{echo "fail";} 

【问题讨论】:

  • 也许你错过了 $destination = "var/www/html/image" 中的起始 / 。 $文件;
  • 执行 if(move_uploaded_file($tmp_url, $destination)){echo "success";}else{echo "fail";} 并发布输出...完成
  • @WilliamMadede:它说失败。
  • 你怎么称呼你的函数uploadToS3??
  • 更新 $destination = "var/www/html/image" 。 $文件;到 $destination = "var/www/html/image/" 。 $文件;

标签: php file-upload


【解决方案1】:

另外请检查目标图片文件夹是否有777 / 775 permission

$ImageName = $_FILES['file']['name'];
$fileElementName = 'file';
$path = 'var/www/html/image/'; 
$location = $path . $_FILES['file']['name']; 
move_uploaded_file($_FILES['file']['tmp_name'], $location);

【讨论】:

    【解决方案2】:

    在上传文件时,以完整路径的方式构建目标总是一个好主意 - 为什么不创建一个全局帮助器来让您访问您的基本路径:

    /**
     * Return the base path.
     *
     * @param void
     * @return string
     */
    function base_path()
    {
        // I go back 2 directories here - it all depends where
        // you decide to place this global helper function.
        return realpath(__DIR__ . '/../..');
    }
    

    太好了,现在它可以在全球范围内使用,您可以决定像这样使用它:

    $destination = base_path() . '/var/www/html/image';
    

    这样,您不必担心当前位置 - 这太容易了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多