使用PHP我试图将图片从一个文件夹移动到另一个文件夹。在我的照片处理脚本中,当我使用rename()时,我在控制台中收到以下错误消息:
Uncaught SyntaxError: Unexpected token <
我的重命名功能就像这样:
rename("../../uploads/temp/users/15/photo-user-15-2015-01-31-13-19-12.jpeg", "../../uploads/final/users/15/photo-user-15-2015-01-31-13-19-12.jpeg");
我已经更改了文件夹的权限&#34; temp&#34;和&#34;最终&#34;到777,但我仍然遇到这个问题。
这是我的完整代码:
<?php
/************************************************
* The Croppic Img Crop To File PHP
* - goal: cropping the image
************************************************/
/* saves cropped image to "temp" folder
* possible GET parameters:
* - subfolder --> contains name of group: users, adverts
*/
session_start();
include '../db.php';
include '../functions.php';
// receive image data
$imgUrl = $_POST['imgUrl'];
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];
$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
// set image quality
$jpeg_quality = 100;
// define output name
$folderUploads = '../../uploads/';
$tempParentDir = $folderUploads . 'temp/' . $_GET['subfolder'] . '/';
$finalParentDir = $folderUploads . 'final/' . $_GET['subfolder'] . '/';
// if user folder does not exist create one
$parentDirWithUserFolder = $tempParentDir . getUserId($db);
if (!is_dir($parentDirWithUserFolder)) {
// Check if the parent directory is a directory
if (!is_dir($tempParentDir)) {
die('Invalid path specified');
}
// Check if the parent directory is writeable
if (!is_writable($tempParentDir)) {
die('Unable to create directory, permissions denied.');
}
// Create the directory
if (mkdir($parentDirWithUserFolder) === false) {
die('Problems creating directory.');
}
}
// make sure only specific types of images get uploaded
$what = getimagesize($imgUrl);
switch (strtolower($what['mime'])) {
case 'image/png':
$img_r = imagecreatefrompng($imgUrl);
$source_image = imagecreatefrompng($imgUrl);
$type = '.png';
break;
case 'image/jpeg':
$img_r = imagecreatefromjpeg($imgUrl);
$source_image = imagecreatefromjpeg($imgUrl);
$type = '.jpeg';
break;
case 'image/gif':
$img_r = imagecreatefromgif($imgUrl);
$source_image = imagecreatefromgif($imgUrl);
$type = '.gif';
break;
default:
die('image type not supported');
}
$pathForDb = getUserId($db) . "/photo-user-" . getUserId($db) . "-" . date('Y-m-d-H-i-s');
$output_filename = $tempParentDir . $pathForDb;
$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$dest_image = imagecreatetruecolor($cropW, $cropH);
imagecopyresampled($dest_image, $resizedImage, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
imagejpeg($dest_image, $output_filename . $type, $jpeg_quality);
// move photo file from temp folder to final folder
$sourcePath = $output_filename . $type;
$finalPath = $finalParentDir . $pathForDb . $type;
rename($sourcePath, $finalPath);
$response = array(
"status" => 'success',
"url" => $output_filename . $type
);
print json_encode($response);
?>
当没有重命名功能时,此脚本可以正常工作。
文件提供的响应来自&#34; Network&#34;:
<b>Warning</b>: rename(../../uploads/temp/users/15/photo-user-15-2015-01-31-13-41-26.jpeg,../../uploads/final/users/15/photo-user-15-2015-01-31-13-41-26.jpeg): No such file or directory in <b>/home/www/system/process-image-upload/process-image-save-to-temp.php</b> on line <b>94</b><br />
该文件肯定存在,我在服务器上查找。我使用重命名($ value1,$ value2)...
答案 0 :(得分:0)
您的重命名可能会引发Exception
,您尝试rename
的文件不存在,或者您没有文件夹操作所需的权限。尝试使用此代码进行测试:
try {
rename($sourcePath, $finalPath);
} catch (Exception $e) {
echo var_dump($e);
}
根据给定的请求查看网络中的错误消息,解决问题并获取测试代码(回显var_dump
)。
答案 1 :(得分:0)
您正在将文件从$tempParentDir
移动到$finalParentDir
,但您要检查源目录是否存在,而不是目标目录。您收到的错误消息表示目标目录不存在。您需要更改代码,以确保目标目录存在,而不是源目录(因为您知道其中有文件,所以始终存在)。
我认为你必须改变这个:
// if user folder does not exist create one
$parentDirWithUserFolder = $tempParentDir . getUserId($db);
if (!is_dir($parentDirWithUserFolder)) {
// Check if the parent directory is a directory
if (!is_dir($tempParentDir)) {
die('Invalid path specified');
}
// Check if the parent directory is writeable
if (!is_writable($tempParentDir)) {
die('Unable to create directory, permissions denied.');
}
// Create the directory
if (mkdir($parentDirWithUserFolder) === false) {
die('Problems creating directory.');
}
}
替换
$parentDirWithUserFolder = $tempParentDir . getUserId($db);
与
$parentDirWithUserFolder = $finalParentDir . getUserId($db);