Jump to content

Recommended Posts

Hello all,

 

well i am working on a project where you upload stories to a db and display them on a page. now i have my upload form page with my text boxes and all. and i can upload the stories and display them but now i want to add images through this upload form page.

 

can anybody point me to the right direction as of how to go about this.

 

Thank you in advance

Link to comment
https://forums.phpfreaks.com/topic/148846-inserting-pictures/
Share on other sites

Generally you should put the picture in a directory then put the file information in a DB. It is not "good" practice to put images in the DB. Although if that sounds preferable you would use the BLOB data type.

 

For more on how to read them back to the page readfile and header for that part.

Link to comment
https://forums.phpfreaks.com/topic/148846-inserting-pictures/#findComment-781626
Share on other sites

A couple things:

 

First, you want to alter the form to include the enctype:

<form id="whatever" action="whatever" enctype="multipart/form-data" >

 

Then, you want your file field in the form itself:

<input type="file" name="image_file" />

 

Next, you may want to do some javascript validation to make sure it's the right file type.

 

If you have a one-to-one relationship between post and image (one image per post), you will add the field to the database.

 

Finally, you edit your form processing script to include the file upload:

 

// set the path
$path = 'path/to/save/filename.jpg';

// handle the upload
move_uploaded_file($_FILES['image_file']['tmp_name'], $path) ;

// enter into database
mysql_query("UPDATE table set `image_path`= '$path');

 

Note that this is VERY simple, and is NOT meant to be a cut and paste solution. You will probably want to do more to validate server-side, check for existing files to avoid over-writing them, and more.

 

A more in-depth discussion can be found here:

http://www.webdeveloper.com/forum/showthread.php?t=101466

 

Hope that helps.

Link to comment
https://forums.phpfreaks.com/topic/148846-inserting-pictures/#findComment-781640
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.