Jump to content

I have a question about file uploading.


Superian

Recommended Posts

1.)What is the best way to store photos?

 

In files.

 

2.)Is it possilbe to send photos straight to the database or

will have to write a script to grab the file from the temp_file?

 

This question has a yes and no answer.  Yes, you can insert them into the database, however, when a file upload occurs php automatically writes it to a temporary file, which is cleaned up automatically after the script concludes execution.

 

So, in order to insert them into a database, you must use one of the file read functions on the file located at the location stored in the 'tmp_name' location in the $_FILES['form_field_array'] array.

Link to comment
Share on other sites

Correct me if I am wrong.

 

I will need to create a script that will upload the

file. Write a script that will open the temp_file,

grab the file and then store it in the database, correct?

 

If so, what type of field will I need as a database column?

 

Note: I am trying to create a photo ablum.

Link to comment
Share on other sites

i would suggest you save the file via the filesystem (ie. as an actual image file) and store the FILENAME in the database.  in this case, it's a matter of setting up its filename, ensuring it's unique, moving the temp file to its final location (move_uploaded_file()), and inserting its entry into the database.

 

have a look in the manual regarding handling file uploads; there's an entire section on it.  should give you some good background.  if you wanted to save the file as its binary format, you'd need a BLOB column in your table.  i've yet to see a situation where this is preferable, though.

Link to comment
Share on other sites

I heavily suggest storing the images in files unless it's a cluster of servers or some other situation....

 

You would want to store it as the 'blob' type.

 

If you want to use a DB cause you don't know how you would map to the files, you could always just store the image name and then use that file for the id in the DB..... Just a suggestion.

 

 

 

Edit:  akitchin covered everything I said, but I typed it, so it's gonna be read ;p.

Link to comment
Share on other sites

I heavily suggest storing the images in files unless it's a cluster of servers or some other situation....

 

You would want to store it as the 'blob' type.

 

If you want to use a DB cause you don't know how you would map to the files, you could always just store the image name and then use that file for the id in the DB..... Just a suggestion.

 

 

Edit:  akitchin covered everything I said, but I typed it, so it's gonna be read ;p.

 

I am building a photo ablum website. By doing this, I am

also trying to save web space during the process.

 

What are the advantages and disadvantages of both storing

photos in a file compared to storing photos in a database. Anyone?

Link to comment
Share on other sites

They are going to use disk space if they are in the database or on the file system...

 

Generally speaking, if you have a shared hosting solution, they limit you to a very small amount of MySQL space...you may want to check on that.

 

Basically, my best option is to create a script that

will produce a file for each user and store the

photos in their file?

Link to comment
Share on other sites

i would suggest you save the file via the filesystem (ie. as an actual image file) and store the FILENAME in the database.

 

As akitchin said, store the file, regardless of name, on the file system, then store a reference (the location) to it in the database.  Then you can store other metadata, like the owner, in the database and access it easily.

Link to comment
Share on other sites

I have writting the entire script where the file

uploads, validates, and resizes the image.

 

The problem I am facing now is storing the name of the

file in the database. How can this be done?

 

// Above is validation and image resize

$photo_path = "php_uploads/";
<?php
if (file_exists("$photo_path" . $PhotoName)){
      echo $PhotoName . " already exists. ";
}
else{
      move_uploaded_file($PhotoTemp, "$photo_path" . $PhotoName);
      echo "Stored in: " . "$photo_path" . $PhotoName;
}	
?>

Link to comment
Share on other sites

I would randomly generate image names and store them above the webroot.

 

I would then log the true image name in the database, along with the owner id, owner name, original image name, and the renamed generated name (along with other stuff, but that's just me).

 

Then I would make a simple script that pulled the true image name from the DB and I would output it to the browser.

 

If users log in to view pictures you could make it where it shows their pictures like that and they could use original names, but most likely you would want to give them some ID for the image to view (or just the renamed name).  Then that could be plugged in to a url and your script could serve the correct image for that ;p.

Link to comment
Share on other sites

I would randomly generate image names and store them above the webroot.

 

I would then log the true image name in the database, along with the owner id, owner name, original image name, and the renamed generated name (along with other stuff, but that's just me).

 

Then I would make a simple script that pulled the true image name from the DB and I would output it to the browser.

 

If users log in to view pictures you could make it where it shows their pictures like that and they could use original names, but most likely you would want to give them some ID for the image to view (or just the renamed name).  Then that could be plugged in to a url and your script could serve the correct image for that ;p.

 

Would this include the file extension also? If so, what would be

the field type and the size of the type?

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.