Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

- - - - -

Tutorial: Storing Images in MySQL with PHP

max_file_size mysql images php

  • This topic is locked This topic is locked
47 replies to this topic

#13 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 18 August 2008 - 04:06 AM

Yes, in the HTML you will need to specify the image variable as an array:


<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input name="image[]" accept="image/jpeg" type="file">
<input value="Submit" type="submit">


Notice the brackets [] on the image name (image[])? That allows you to upload multiple images via HTML. In PHP you will need to iterate through the image[] array.
  • 0

#14 s1gh

s1gh

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 23 September 2008 - 03:11 PM

I can't get this script to work.
Everytime I try to upload an image, I get the following error: No image selected/uploaded.

Is there a problem with the script?

Edit: Stupid me. I had set the maximum filesize way too low! ;)
So now it's working! :)

Thanks for the tutorial! :)
  • 0

#15 shailesh

shailesh

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 17 December 2008 - 03:45 AM

it is a good tutorial and code for php learner
  • 0

#16 moulai

moulai

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 18 February 2009 - 01:06 AM

hi

retrieve the contents from database, and save it in a image formated file, call it from html
  • 0

#17 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 30 May 2009 - 08:27 PM

Hi I have followed all the steps and I understand the concept behind uploading and displaying an image(according to Jaan), however the image still does not display for me.

I believe the problem is with the rights of the file or something. The reason why I think so is because i have tried uploading an image to the "uploads" folder with ftp and i can easily display it however if i upload the same exact image with the upload form and then i try to view it I get the following message "403 Forbidden".

If it makes any difference I am using a hosted site.

Any idea why when i upload an image with the upload form it becomes forbidden in the folder?

Thank you Very much in advance
  • 0

#18 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 31 May 2009 - 05:18 AM

A 403 means you can't access your PHP script. You probably have bad permissions, as you stated. chmod 755 it and try again.
  • 0

#19 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 31 May 2009 - 05:37 AM

So what permission rights must a folder technically have?

Initially i had the following permissions:

(and if you chmod 755 it this is how permissions will end up)

Me: Read and Write
Staff: Read only
Everyone: Read only

then I changed them to
Me: Read and Write
Staff: Read and Write
Everyone: Read and Write

and still get the forbidden message.

Btw I have the following setup:

mywebsite.com contains a folder "uploads"
mywebsite.com contains all of my php pages

I am trying to store just the path in the database and the actual image files
in the upload folder.

I host my website, so in order to make sure that my folder(uploads) had read and write access rights(which i assume are correct) i have created it on my computer and then sent it with ftp to my host. So I assume the folder still has the correct permission rights set up.


It is weird that it works fine if i ftp and image (i am then able to view it). But if i use the uploads form it does get uploaded but i cant view it and get that "forbidden" message.

Thank you so much for your help!
  • 0

#20 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 31 May 2009 - 07:18 AM

Hi Jordan! thanks for help.

So I discovered that after I upload my image with the file upload form, the actual image's permission settings change. They change to no read rights. Therefore somewhere in my upload form i have to chmod 755 the actual file but I am not sure how to do so.

Could you please help me out? and here is the code I have to actually upload the image:

if(isset($_POST['submitted']))
{

$uploadDir = 'uploads/';

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result)
{
echo "Error uploading file";
}

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
}

Edited by Jaan, 31 May 2009 - 03:29 PM.
Please use code tags when you are posting your codes!

  • 0

#21 eXcellion

eXcellion

    CC Lurker

  • Just Joined
  • Pip
  • 7 posts

Posted 01 June 2009 - 02:18 AM

A question about uploading an image to my database.
(same question as in other topic, but I need to finish it within 12 hours..)

I used the tutorial recommended above, but it seems as if I'm to plain dumb to get it to work, I followed the instructions, but it's not working, I get errors about these lines:
      $fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


Warning: fread(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 26

Warning: fclose(): supplied argument is not a valid stream resource in /home/vhosting/21/sintpietersgent.be/www/leerlingenraad/seminarie/insert.php on line 28


This is the entire code:

bestellingenCP.php
<table>
<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<tr><td>Afbeelding:</td><td><input name="image" accept="image/jpeg" type="file"></td></tr>
<tr><td>Naam:</td><td><input name="naam" type="text" /></td></tr>
<tr><td>Prijs:</td><td><input name="prijs" type="text" /></td></tr>
<tr><td>Omschrijving:</td><td><textarea name="omschrijving" ></textarea></td></tr>
<tr><td></td><td><input value="Submit" type="submit"></td></tr>
</form>
</table>


insert.php (the problem..)

<?php
$host="localhost";
$name = "correct";
$pass = "correct";
$dbname = "correct";

$dbi = mysql_connect($host, $name,$pass) or
die("Kan niet verbinden met de database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

if (isset($_FILES['image'])) {


$tmpName = $_FILES['image']['tmp_name'];


$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
$naam = $_POST['naam'];
$prijs = $_POST['prijs'];
$omschrijving = $_POST['omschrijving'];


$query = "INSERT INTO bestellingen (naam,prijs,beschrijving,image) VALUES
('$naam','$prijs','$omschrijving','$data')";
$results = mysql_query($query, $dbi);

}
else {
print "No image selected/uploaded";
}
mysql_close($dbi);
?>

  • 0

#22 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 01 June 2009 - 04:27 PM

A note referring to my previous post:

I learned that an upploaded file ( $_FILES['userfile']['tmp_name'] ) has permission 600 and that I have to use chmod() to change permission.

Do you know where and how i would apply it?


Thank you in advance for your replies! I really appreciate it
  • 0

#23 Guest_Jaan_*

Guest_Jaan_*
  • Guest

Posted 02 June 2009 - 05:17 AM

Use this function PHP: chmod - Manual when you have uploaded this image.. just after move_uploaded_file() function..
  • 0

#24 ssSuave

ssSuave

    CC Newcomer

  • Just Joined
  • PipPip
  • 12 posts

Posted 02 June 2009 - 07:01 PM

Thanks a Lot buddy!
  • 0





Also tagged with one or more of these keywords: max_file_size, mysql, images, php