00stuff Posted September 18, 2010 Share Posted September 18, 2010 Hi guys, I have this code that I created. It is suppost to get a file from a form and upload it to a directory that is outside the root folder of my webserver. then the image name is added to the preset location of the directory that contains all of the uploaded images and is placed into a mysql database so I can call it later. the problem I have is that when some one else signs up through this form and the file they upload has the same file name it replaces the one that is already in the images directory that has all the other pictures. I need to add something to this code that adds $username to the end of the original file name and then uploads it to the directory with the new name. Since every username is unique it will make every username unique and will eliminate my problem. I am not sure how to do this. Please, anyone that can help. Thanks. This is my code. // get file attributes!!!!! $photoname = $_FILES['photo']['name']; $tmp_name = $_FILES['photo']['tmp_name']; if ($photoname) { // start upload process $location = "../userpictures/$photoname"; move_uploaded_file($tmp_name,$location); } else { $location = "../userpictures/nophoto.png"; } $queryreg = mysql_query(" INSERT INTO users VALUES ('','$fullname','$username','$password','$date','$location','$email','$phone') "); If there is a better way to upload images to a mysql database and outputting it later then please let me know as well. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/ Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 $location = "../userpictures/$photoname"; shouldn't that be: $location = "../userpictures/" . $photoname . "; ?? Excuse me if I am wrong, I'm fairly new. Anyways, just call it from the SQL DB and then echo it on the user-page like this <?php mysql_connect("localhost", "NAME", "PASSWORD") or die("Could not connect: " . mysql_error()); mysql_select_db("DB_NAME"); $result = mysql_query("SELECT fullname, username, location, email FROM threads ORDER BY threadid DESC"); while ($row = mysql_fetch_array($result, MYSQL_NUM)) { printf("<br />Full name: %s<br />Username: %s<br />Location: <img src="%s"<br />Email: %s", $row[0], $row[1], $row[2], $row[3]); } //is it a local file or a url mysql_free_result($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112356 Share on other sites More sharing options...
Pikachu2000 Posted September 18, 2010 Share Posted September 18, 2010 $location = "../userpictures/$photoname"; shouldn't that be: $location = "../userpictures/" . $photoname . "; ?? Excuse me if I am wrong, I'm fairly new. No, it shouldn't; it has a parse error. The way the OP has it is fine. As far as calling anything from a database, the OP is trying to upload files, not display them. Anyhow, if you want to add the username to the filename, I'd suggest prepending it with an underscore. Makes it easier to read, and easier to sort the directory listing when necessary. Apparently you already have the username stored in a variable for the INSERT query, so this should work. Note that this still won't eliminate file overwrites if the user uploads two different files that have the same name, however. $location = "../userpictures/$username_$photoname"; Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112361 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 Thanks but it still doesn't solve my whole problem. The answer you gave me adds the username to the location of the file, but the file is still being uploaded to the directory with the original name. I need the code to upload the file and change the file name while uploading it and send that modified file name to the location field on my database. Any ideas??? Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112367 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 Thanks but it still doesn't solve my whole problem. The answer you gave me adds the username to the location of the file, but the file is still being uploaded to the directory with the original name. I need the code to upload the file and change the file name while uploading it and send that modified file name to the location field on my database. Any ideas??? Not a clue.... Couldn't you make it upload to a folder on the host like "images" then make it generate a random name? Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112368 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 The image is already being uploaded to the folder "userpictures" but I need the file to uploaded there with the modified name already. that way the location will show the actual image name. Do you understand? Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112376 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 The image is already being uploaded to the folder "userpictures" but I need the file to uploaded there with the modified name already. that way the location will show the actual image name. Do you understand? Yeah, I get it now. I thought you were just using img src and uploading a string to SQL db. (Teaches me not to read the entire thread :x) Hmm, you could just md5 the name to save you time.. $oldname = $_POST['FILENAMEFORM'] $newname = md5($oldname) Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112378 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 $location = "../userpictures/$photoname"; shouldn't that be: $location = "../userpictures/" . $photoname . "; ?? Excuse me if I am wrong, I'm fairly new. No, it shouldn't; it has a parse error. The way the OP has it is fine. As far as calling anything from a database, the OP is trying to upload files, not display them. Anyhow, if you want to add the username to the filename, I'd suggest prepending it with an underscore. Makes it easier to read, and easier to sort the directory listing when necessary. Apparently you already have the username stored in a variable for the INSERT query, so this should work. Note that this still won't eliminate file overwrites if the user uploads two different files that have the same name, however. $location = "../userpictures/$username_$photoname"; Oh, okay. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112380 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 That's fine. I can encrypt the name, but what about the file? I need it to change the name of the file remember, not just the location that is uploaded to the database. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112381 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 That's fine. I can encrypt the name, but what about the file? I need it to change the name of the file remember, not just the location that is uploaded to the database. >implying you have any php skills at all. the first part (oldname) is the name of the file in the first place, then, oldname (which encrypts $oldname). Where you save it to the folder, instead of it being $newname, change the var to $oldname. Do you understand now? Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112383 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 Where in my script do you see "$_POST" ???? Do you have any idea of what you are saying? It's an image being uploaded. You must use "$_FILES". Show me what you mean? Write out the script from the beginning of my post with your solution. PLease. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112388 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 Where in my script do you see "$_POST" ???? Do you have any idea of what you are saying? It's an image being uploaded. You must use "$_FILES". Show me what you mean? Write out the script from the beginning of my post with your solution. PLease. I will not spoon-feed you. If you have a basic understanding of PHP you should know what to do. EDIT: okay.. I give in. $location = "../userpictures/$newname"; As I understand you are using $_FILES.. Since as far as I know the only way that my method would work is if you gave the user a text-box with a name for their file Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112389 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 LMFAO!!! That doesn't change the file name... It only changes the file location. Please only reply if you really know what you are talking about. Don't worry. I found a way. Thanks anyways. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112390 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 // get file attributes!!!!! $photoname = $_FILES['photo']['name']; $tmp_name = $_FILES['photo']['tmp_name']; $photoname = $username . $photoname; if ($photoname) { // start upload process $location = "../userpictures/$photoname"; move_uploaded_file($tmp_name,$location); } else { $location = "../userpictures/nophoto.png"; } $queryreg = mysql_query(" INSERT INTO users VALUES ('','$fullname','$username','$password','$date','$location','$email','$phone') "); Adding that line of code changed both the file name and the location that was uploaded to the database. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112391 Share on other sites More sharing options...
Username: Posted September 18, 2010 Share Posted September 18, 2010 LMFAO!!! That doesn't change the file name... It only changes the file location. Please only reply if you really know what you are talking about. Don't worry. I found a way. Thanks anyways. ...$oldname is = to photoname lol this is why I implied earlier that instead of using $_POST replace it what you were using before. Oh well, glad you solved it . Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112393 Share on other sites More sharing options...
Pikachu2000 Posted September 18, 2010 Share Posted September 18, 2010 If you had bothered to try what I posted, you would have seen that it did the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112430 Share on other sites More sharing options...
00stuff Posted September 18, 2010 Author Share Posted September 18, 2010 Thank you guys. I needed some fresh ideas. I was too tired. I just didn't understand what you guys meant. I appreciate your help. Quote Link to comment https://forums.phpfreaks.com/topic/213707-uploading-image-with-php-into-mysql-database/#findComment-1112498 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.