With PHP I am trying to move a picture from one folder to another. In my photo processing script, when I use rename(), I get the following error message in my console:
Uncaught SyntaxError: Unexpected token <
My rename function is something like this for example:
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");
I already changed the permissions for the folders "temp" and "final" to 777, but still I get this problem.
Here comes my full code:
<?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);
?>
This script works perfectly when WITHOUT the rename function.
Here comes the response the file provides under "Network":
<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 />
The file definitely exists, i looked it up on the server. I use rename($value1, $value2)...
You are moving a file from $tempParentDir
to $finalParentDir
but you check whether the source directory exists, not the destination directory. The error message you got means that the destination directory does not exist. You need to change your code so that it ensures the destination directory exists, not the source directory (which always exists as you know there is a file in it).
I think you just have to change this:
// 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.');
}
}
Replace
$parentDirWithUserFolder = $tempParentDir . getUserId($db);
with
$parentDirWithUserFolder = $finalParentDir . getUserId($db);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments