"Uncaught SyntaxError: Unexpected token <" when using rename() function

Claudius Rehme

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)...

Tobias

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.

edited at
0

Comments

0 comments
Login to comment

Related

"Uncaught SyntaxError: Unexpected token import" when using momentjs with ASP5/MVC6

Uncaught SyntaxError: Unexpected token < when using bootstrap js

"Uncaught SyntaxError: Unexpected token <" when trying to use content of Rails array in Javascript

React Router - Uncaught SyntaxError: Unexpected token < when refreshing an URL with params

Function Constructor - add function using prototype gives - Uncaught SyntaxError: Unexpected token {

Uncaught SyntaxError: Unexpected token { when importing an array

Uncaught SyntaxError: Unexpected token when using leaflet with flask

JS: Uncaught SyntaxError: Unexpected token '<' in browser console when using <?php echo json_encode($_SESSION['

Uncaught SyntaxError: Unexpected token ? using Zepto

Uncaught SyntaxError: Unexpected token . when using ClassNotty

Uncaught SyntaxError: Unexpected token ')' in underscore when looping throught template

Javascript - Uncaught SyntaxError: Unexpected token ) when calling a function

Angular directive calls function passed by isolated scope (ngclickfunction:'&') works fine but browser gives: Uncaught SyntaxError: Unexpected token }

Uncaught SyntaxError: Unexpected token e when try to parse a JSON string

Uncaught SyntaxError: Unexpected token < using for loop in video js thumbnail

Uncaught SyntaxError: Unexpected token , pass multiple variables to javascript function

'Uncaught SyntaxError: Unexpected token x in JSON at position 1' when trying to parse HTML embeded string JSON

Javascript object member function: "Uncaught SyntaxError: Unexpected token ," (Chromium)

Uncaught SyntaxError: Unexpected token ) when i am trying to get html from json

Received 'Uncaught SyntaxError: Unexpected token import' when executing Karma and Jasmine Test

Uncaught SyntaxError: Unexpected token . when setting values to firebase database

Get this error : "Uncaught SyntaxError: Unexpected token <" when i call a function trough onclick

Uncaught SyntaxError: Unexpected token { function js

Uncaught SyntaxError: Unexpected token & when passing list/array to Javascript

Uncaught SyntaxError: Unexpected token ( while running JS function

Google Search Console Error: Uncaught SyntaxError: Unexpected token function

Uncaught SyntaxError: Unexpected token '%', Flask error when using a separate js file

Uncaught SyntaxError: Unexpected token l in JSON when converting json string to object JS

Uncaught SyntaxError: Unexpected token 'function'