我正试图使用以下代码将图像从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";}
此外,请检查目标图像文件夹是否具有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);
在上传文件时,最好以全路径方式构建目的地-为什么不创建一个全局帮助程序,让您可以访问基本路径:
/**
* 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';
这样,你就不必担心你现在的位置——这太容易了。