![]() |
Change filename on upload
Hi,
I've got a form that lets users upload a thumbnail file. It then puts the filename of the image in the database. The code looks like: Code:
<?phpAlso how would I go about resizing the image if it is over 100px x 100px? |
If you output the $_FILES with print_r();, you'll see that it is a multidimensional array. Thus, the key names 'name', can simply be changed by setting the value $_FILES['uploadfile']['name'] with a prefix. Automaticly generated by choice, of course.
|
Thanks, I've got the image name sorted.
Any ideas on resizing it if it's too big? |
You can change the name in the move_uploaded_file(), as a second argument just specify the filename (and path) you desire.
In this script you don't check for safety, you should always check the Mimetype and extention of the file you are uploading. Here a little function i made a while ago, i recommend you check this function for ideas how to make your script be save. (Oh and i also included a little function that makes tumbnails) PHP Code:
|
Thanks for that :)
|
Nice & Dutch Jim. :P Hehe, lovely translated.
Anyways, are you somewhat good in RegEx? |
What I do when uploading files is, use touch() then file_put_contents. :P
|
How important is checking mime types?
For some reason some of the images I try to upload dont have mime types according to my script. I also read this on the php.net site: Quote:
|
Hmm I always learned to also check the mimetype because people would be able to just upload any file thats a .jpg for example.
Of course I don't know how that could harm you server in any way since it won't be read as (for example) an executable. But it doesn't harm adding in your script so I've done it ever since. I never had problems with finding the mimetypes from a file though. In IE6/7 FF1/2 and Opera 9 I could just upload the files and the mimetype would be found. |
How are you checking the mime-type? I've never seen a browser that sends a file without that type of header information, but I'm sure they could exist. You can alternatively double-check your image uploads with
getimagesize() or a combination of exif_imagetype() and image_type_to_mime_type() (which coincidently also received the award for function name with the most underscores, PHP Grammies 2005)...-m |
To check the mime type I'm using:
Code:
$mimetypes = array('image/jpeg', 'image/png', 'image/pjpeg', 'image/gif'); |
The mime-type should be all lowercase to begin with, but it doesn't hurt to make sure I suppose. Try throwing a
die($_FILES['uploadedfile']['type']); or just echo it to see if there's any actual data in there to begin with (not that strtolower() should somehow delete it, but that's how fun debugging is). If you're not seeing any data there, but you are in other parts of $_FILES['uploadedfile]', tryPHP Code:
|
That doesn't seem to work either, I'm getting really confused now *!*
Here's all my code for you to have a look at (as I'm probably doing something really stupid). PHP Code:
|
| All times are GMT. The time now is 11:55 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0