/    Sign up×
Community /Pin to ProfileBookmark

file upload help, limiting file types and changing file name upon upload

hi i am working on an upload script, this is what i have so far

$file_dir = “/home/richardt/public_html/”;

foreach( $HTTP_POST_FILES as $file_name => $file_array ) {
print “path: “.$file_array[‘tmp_name’].”<br>n”;
print “name: “.$file_array[‘name’].”<br>n”;
print “type: “.$file_array[‘type’].”<br>n”;
print “size: “.$file_array[‘size’].”<br>n”;

$file_name = $file_array[‘name’];

if( is_uploaded_file( $file_array[‘tmp_name’] ) ) {
move_uploaded_file( $file_array[‘tmp_name’], “$file_dir/$file_name”) or die (“Couldn’t Copy”);
}
}

now this will upload the file into the directory specified with the filename of what it was on the users computer

however a title was entered on the same page as they selected the file (on the form), and i would like the file name to just be

$HTTP_POST_VARS[“new”] . extension of what it is (jpg only upload)

eg

Title = Foo
Picture = xplogo.jpg

Click Add, it uploads

xplogo.jpg is on my server but called foo.jpg

also if its not too much to ask i only want to be able to upload a jpg, i can do it js but would prefer to use php, i think i can code this bit, so ill give it a try and post back if i get it, many thanks

Rich

//Edit ive managed to limit upload types
if(preg_match(“/.jp/i”, $file_array[‘name’])){

$file_name = $file_array[‘name’];

if( is_uploaded_file( $file_array[‘tmp_name’] ) ) {
move_uploaded_file( $file_array[‘tmp_name’], “$file_dir/$file_name”) or die (“Couldn’t Copy”);
}

} else {

echo “Invalid file type, please <a href=javascript:history.go(-1);>go back</a> and upload a .jpg file”;
exit();

}
}

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrMay 23.2004 — If you have the Mimefunctions extension available to you, you can more accurately check to make sure you are only uploading .jpg images:
[code=php]
if(mime_content_type($file_array["name"]) != "image/jpg") {
echo "Dude, only .jpg's!";
}
[/code]


As for renaming the file, just use the good ol' [url=http://www.php.net/rename]rename();[/url] function after uploading the file:
[code=php]
if( is_uploaded_file( $file_array['tmp_name'] ) ) {
move_uploaded_file( $file_array['tmp_name'], "$file_dir/$file_name") or die ("Couldn't Copy");
rename($file_dir . "/" . $file_name, $file_dir . "/" . $HTTP_POST_VARS["new"] . substr($file_name, -4, 4));
}
[/code]

That uploads the file, then renames it and makes sure the file extension of the old name is appended to the new one, so everything is kosher. (Note, this is untested, but it should work.)
Copy linkTweet thisAlerts:
@96turnerriauthorMay 23.2004 — thanks for that i changed the code to mime type, and i found rename() late yesterday and applied that code and everything is working fine now, Thanks
Copy linkTweet thisAlerts:
@Paul_JrMay 24.2004 — You&#8217;re welcome. Glad you got everything working. ?
×

Success!

Help @96turnerri spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...