Page 1 of 1

maintain image name solved

Posted: Sun Jan 17, 2010 2:46 pm
by scarface222
Hey guys quick question, does anyone know a method to maintain the image name while uploading? When I say that, I mean I am uploading images to a folder and storing url in database, and it seems the only way that most images will only be linked properly if I use rawurlencode() and that destroys the name integrity. urlencode() isn't as bad but does not work on all images for example this one for some reason 'DMX - It's Dark And Hell Is Hot'.

This is my script when the file is done checking and ready to upload

Code: Select all

    $image1=rawurlencode($_FILES['Filedata']['name']);
$second_query = "INSERT INTO chat_images
(username, image) VALUES('$username', '$image1' ) ";
mysql_query($second_query) or die('Error, insert query failed');
 
move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploads.'/'.rawurlencode($_FILES['Filedata']['name']))
or die ("Couldn't upload ".$_FILES['Filedata']['name']."\n");

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 2:49 pm
by ShadowIce
use php sessions?

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 2:54 pm
by scarface222
Could you maybe be more specific lol? I am still quite new to programming. I currently use sessions for login purposes but I do no know how that will help me maintain the image title.

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 3:01 pm
by ShadowIce
edit =D

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 3:15 pm
by scarface222
lol why did you edit it? Anyway I downloaded your file, and I appreciate the tutorial, funnily enough it did help me feel more comfortable with sessions but it didn't really address my problem. I am talking about uploading an image or mp3 title into my database, and when I encode it, the title is changed beyond recognition from added characters, so I was wondering if there was an alternative, or function that would make the file url compatible, but readable by the original title.

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 3:18 pm
by mellowman
ShadowIce wrote:use php sessions?
Sessions??? You pass the name of the file when u submit it to your upload script...what would be the point of sessions :mrgreen:

...have u tryed to remove special characters before its uploaded?...the problem your having is most likely to be the coma in the name of the image :mrgreen:

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 3:19 pm
by ShadowIce
I'm not so sure on that sorry mate. Only thing I DO know is, it has something to do w/ how the password system works. It's the same idea. It goes in as a password hash, and comes out as a decryption when u login to a site :) I would start there! :)

Re: maintain image name when uploading, quick question

Posted: Sun Jan 17, 2010 3:38 pm
by scarface222
Yeah I just used preg_replace lol, I feel kind of dumb now for posting this now, but I hadn't used that function before, thanks mellow, and thanks shadow for your session tutorial, it did help me in a different way but I think you may be over using sessions lol. This is the code I used to accomplish this

$strfile=$_FILES['Filedata']['name'];
$image1=preg_replace("/[^a-zA-Z0-9\.]/","-",$strfile);

Re: maintain image name solved

Posted: Sun Jan 17, 2010 4:39 pm
by mellowman
glad to help :mrgreen:

if u need more help hit me up on aim

aim-imcmellowman

Re: maintain image name solved

Posted: Sun Jan 17, 2010 6:52 pm
by scarface222
Yeah that would be cool, does msn messenger work with aim?

Re: maintain image name solved

Posted: Sun Jan 17, 2010 7:08 pm
by mellowman
scarface222 wrote:Yeah that would be cool, does msn messenger work with aim?
i have no idea lol

Re: maintain image name solved

Posted: Sun Jan 17, 2010 7:10 pm
by scarface222
I will get aim in a bit lol and add you, it will be like my programming talk messenger.

Re: maintain image name solved

Posted: Sun Jan 17, 2010 8:21 pm
by McInfo
Why not store the intact filename in the database? Format it with mysql_real_escape_string() to make it safe to use in the insert query. When you retrieve it from the database, URL-encode it if you need to use it in a hyperlink.

Edit: This post was recovered from search engine cache.

Re: maintain image name solved

Posted: Sun Jan 17, 2010 8:28 pm
by scarface222
thats a good point, for some reason I was having trouble with some image names and urlencode, but I was using it before database instead of that preg statement. Probably just an error on my part. Great idea and Thanks for reminding me, I can't believe I actually forgot the escape statement.