jeremyBass26
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
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
oops...,
make
$targetFile[count($targetF ile)-1)] = "_photo";
into
$targetFile[count($targetF ile)-1)] .= "_photo";
so the dynamic part does not get overwrited.
make
$targetFile[count($targetF
into
$targetFile[count($targetF
so the dynamic part does not get overwrited.
ASKER
Dang that didn't work, the file never got saved ... no error thou
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
jeremy
ASKER
ok... I've been trying everything on this...
from this
$newName = 'new filename here';
move_uploaded_file($tempFi le,$target File);
rename($targetFile,$newNam e);
should I not expect rename($targetFile,$newNam e);
to be
rename('/uploads/feusers/p hoto.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/theDynam icVAR_Phot o.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... ?
from this
$newName = 'new filename here';
move_uploaded_file($tempFi
rename($targetFile,$newNam
should I not expect rename($targetFile,$newNam
to be
rename('/uploads/feusers/p
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/theDynam
????
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... ?
ASKER
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($tempFi le,$target File);
rename($targetFile,$newNam e);
$string = $targetFile;
$pattern = '/(!:?\/uploads\/feusers\/
$replacement = 'newName_Photo';
$newName = preg_replace($pattern, $replacement, $string);
move_uploaded_file($tempFi
rename($targetFile,$newNam
ASKER
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($tempFi le,$target File);
rename($targetFile,$newNam e);
$string = $targetFile;
$pattern = '/(?:\/uploads\/feusers\/)
$replacement = 'newName_Photo';
$newName = preg_replace($pattern, $replacement, $string);
move_uploaded_file($tempFi
rename($targetFile,$newNam
ASKER
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
http://www.solmetra.com/scripts/regex/index.php
with
/(\/uploads\/feusers\/)(.*
/uploads/feusers/testimg21
$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
ASKER
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/fe users/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
so this is what I got
/var/www/vhosts/domain.com
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']
?>
ASKER
What I need to do is get this to be generalized too.
(\/var\/www\/vhosts\/domai n\.com\/ht tpdocs\/up loads\/feu sers\/)
the path can and may change....
oh so close... :)
(\/var\/www\/vhosts\/domai
the path can and may change....
oh so close... :)
ASKER
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
ASKER
ok... figured this may make it esaier to figureout...
$newNameF = preg_replace('/(\/var\/www \/vhosts\/ domain\.co m\/httpdoc s\/uploads \/feusers\ /)(.*?)([\ .*])/', $1$NEWname$3, $targetFile);
still not getting it right yet... thou thetest tool says it is... fun fun
$newNameF = preg_replace('/(\/var\/www
still not getting it right yet... thou thetest tool says it is... fun fun
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window