PHP文件上传/移动不起作用

时间:2015-12-01 09:03:43

标签: php file-upload

我尝试使用以下代码将图像从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

在下面的评论中,我尝试了以下的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";} 

2 个答案:

答案 0 :(得分:0)

另外,请检查目标图片文件夹是否有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);

答案 1 :(得分:0)

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

/**
 * 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';

通过这种方式,您不必担心当前的位置 - 这太简单了。