Jump to content

Uploading image with php into mysql database.


00stuff

Recommended Posts

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.

Link to comment
Share on other sites

 $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);
?>

 

Link to comment
Share on other sites

 $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";

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

 $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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

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  :D.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.