Link to home
Start Free TrialLog in
Avatar of jeremyBass26
jeremyBass26Flag for United States of America

asked on

Change file name on upload

Hello I need to change this script so I can have a dynamic name... the script is from here

http://www.ronniesan.com/articles/jquery-multiple-file-upload.php

I have made all my other changes as needed but I can't seem to change the name of the file.  I want the file to be like such when put in the folder..

2893_Photo.jpg

but I only get the real name.  "2893" is the dynamic part, and I'm sure I can figure that out once i figure out how to correctly change the name, proble need to put a post or something.  Any help here would be great thank you

Jeremy



// JQuery File Upload Plugin v1.4.3 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	// Uncomment the following line if you want to make the directory if it doesn't exist
	// mkdir(str_replace('//','/',$targetPath), 0755, true);
	
	move_uploaded_file($tempFile,$targetFile);
}
echo '1'; // Important so upload will work on OSX

Open in new window

Avatar of phpmonkey
phpmonkey

try this.
// JQuery File Upload Plugin v1.4.3 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
        $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
        
        // Uncomment the following line if you want to make the directory if it doesn't exist
        // mkdir(str_replace('//','/',$targetPath), 0755, true);
        
 
        $targetFile = explode(".",$targetFile);
        $targetFile[count($targetFile)-1)] = "_photo";
        $targetFile = implode(".",$targetFile);
 
        move_uploaded_file($tempFile,$targetFile);
}
echo '1'; // Important so upload will work on OSX

Open in new window

oops...,

make
 $targetFile[count($targetFile)-1)] = "_photo";
into
$targetFile[count($targetFile)-1)] .= "_photo";

so the dynamic part does not get overwrited.
Avatar of jeremyBass26

ASKER

Dang that didn't work, the file never got saved ... no error thou
SOLUTION
Avatar of phpmonkey
phpmonkey

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello, so if I just leave what I posted, the file sget through, but they are the orginal name of the file uploaded.  I have been trying everything, srt_replace to the rename, but the file never ends up in the folder or anywhere else.  I can't echo from there, it's like an ajax call but via flash.  Not to sure what to do just yet.  thanks for the help...

jeremy
ok... I've been trying everything on this...


from this


    $newName = 'new filename here';
    move_uploaded_file($tempFile,$targetFile);
    rename($targetFile,$newName);

should I not expect rename($targetFile,$newName);

to be

rename('/uploads/feusers/photo.jpg','new filename here');

so that would not work... so I'd need to replace

$targetFile at 'photo' with 'theDynamicVAR_Photo'

leaveing '/uploads/feusers/' and '.jpg'

so I'd end up with

'/uploads/feusers/theDynamicVAR_Photo.jpg'

????

too which I'd get the 'theDynamicVAR' via a post or get.  

Thanks for the help... this one is not working out to well lol... may-be reg_ex is needed here... ?



so I should have something like this I'd think... thou the reg_ex is off...


      $string = $targetFile;
      $pattern = '/(!:?\/uploads\/feusers\/)(.)(!:?)/';
      $replacement = 'newName_Photo';
      $newName = preg_replace($pattern, $replacement, $string);
 
    move_uploaded_file($tempFile,$targetFile);
    rename($targetFile,$newName);
I just keep trying ... I think this is the right way to do it as the file still up load, it's just not being renamed i think due to the fact that the pattern is not matching up... this is where im at...

      $string = $targetFile;
      $pattern = '/(?:\/uploads\/feusers\/)(.*?)(?:\.*$)/';
      $replacement = 'newName_Photo';
      $newName = preg_replace($pattern, $replacement, $string);
       move_uploaded_file($tempFile,$targetFile);
      rename($targetFile,$newName);
Ok.. so I tested and tested some expressions and finaly got a match using

http://www.solmetra.com/scripts/regex/index.php

with

/(\/uploads\/feusers\/)(.*?)([\.*])/

/uploads/feusers/testimg21.jpg

$1 1234_Photo $3


which produced  
/uploads/feusers/ 1234_Photo .jpg

yeah... but I plug that in and nothing...  It's not working, so I tryed to  write the vars to a file so I could test it... but that is not working... so I'm a little blind and I don't know where I went wronge... and ideas?

thanks for the help...
jeremy
// JQuery File Upload Plugin v1.4.3 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	// Uncomment the following line if you want to make the directory if it doesn't exist
	// mkdir(str_replace('//','/',$targetPath), 0755, true);
	
	//move_uploaded_file($tempFile,$targetFile);
	
	$myFile = "/var/www/vhosts/*.com/httpdocs/lib/JS/picupload/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $targetFile;
fwrite($fh, $stringData);
fclose($fh);
 
 
    move_uploaded_file($tempFile,$targetFile);
	$string = $targetFile;
	$pattern = '/(http:\/\/www.*.com\/uploads\/feusers\/)(.*?)([\.*])/';
	$replacement = $1 . $_GET['Fname'] . $3;
	$newName = preg_replace($pattern, $replacement, $string);
    rename($targetFile,$newName);
 
}
echo '1'; // Important so upload will work on OSX

Open in new window

Oh I'm close now.. I got the fiel to wirte so I can se what th path is...
so this is what I got

/var/www/vhosts/domain.com/httpdocs/uploads/feusers/025.jpg

If I can just get the last part I'm all set... just have to rename... hard when I have nothing to see errors with lol...

thanks for the help
jeremy
<?php
// JQuery File Upload Plugin v1.4.3 by RonnieSan - (C)2009 Ronnie Garcia
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	// Uncomment the following line if you want to make the directory if it doesn't exist
	// mkdir(str_replace('//','/',$targetPath), 0755, true);
	
	//move_uploaded_file($tempFile,$targetFile);
 
$fp = fopen('testFile.txt', 'w');
fwrite($fp, $targetFile);
fclose($fp);
 
 	$string = $targetFile;
	$pattern = '/(\/var\/www\/vhosts\/domain\.com\/httpdocs\/uploads\/feusers\/)(.*?)([\.*])/';
	$replacement = $1 . 'test_Photo' . $3;
	$newName = preg_replace($pattern, $replacement, $string);
    move_uploaded_file($tempFile,$targetFile);
    rename($targetFile,$newName);
 
}
echo '1'; // Important so upload will work on OSX $_GET['Fname']
?>

Open in new window

What I need to do is get this to be generalized too.

(\/var\/www\/vhosts\/domain\.com\/httpdocs\/uploads\/feusers\/)

the path can and may change....

oh so close... :)
Man I'm just dancing around that reg_ex... just not sure...
if (!empty($_FILES)) {
	$tempFile = $_FILES['Filedata']['tmp_name'];
	$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
	$targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
	
	// Uncomment the following line if you want to make the directory if it doesn't exist
	// mkdir(str_replace('//','/',$targetPath), 0755, true);
$NEWname = $_GET['fname'];	
	//move_uploaded_file($tempFile,$targetFile);
move_uploaded_file($tempFile,$targetFile);
 	$string = $targetFile;
	$pattern = '/(\/var\/www\/vhosts\/domain\.com\/httpdocs\/uploads\/feusers\/)(.*?)([\.*])/';
	$newNameF = preg_replace($pattern, $1$NEWname$3, $string);
 
$fp = fopen('testFile.txt', 'w');
fwrite($fp, $newNameF);
fclose($fp);
 
 
    rename($targetFile,$newNameF);
 
}
echo '1'; // Important so upload will work on OSX 

Open in new window

ok... figured this may make it esaier to figureout...

$newNameF = preg_replace('/(\/var\/www\/vhosts\/domain\.com\/httpdocs\/uploads\/feusers\/)(.*?)([\.*])/', $1$NEWname$3, $targetFile);

still not getting it right yet... thou thetest tool says it is... fun fun
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial