Image Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Image Help

Post by Russ »

Okay, I have an album review section on my site, I will be able to do everything apart from the album cover image, I dunno what to do for that, weather to upload it to mysql, or to upload it to a folder, how would I go about doing that??
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

file uploads, images, etc.

Post by musashi »

Many people argue against the notion of storing binary data in mysql (for images, exec, etc). I think in most cases it is perfectly fine. I would say that storing binary data in the db can clog up the db a lot, and can slow things down if you do it to much.

That being said, lets start with the first part... getting the data on the system.

First in your HTML you will need a form like this:

<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="picture">
<input type="submit" value="Send Picture">
</form>

This when this form is submitted, PHP will receive a $picture variable (assuming you have register_globals turned on, otherwise it comes in as $HTTP_POST_VAR[picture]). Next thing you need to do, since this file will be submitted to a temp default directory, is copy to the location you want on your server:

// $picture_name is a special thing PHP automatically does, it actually will
//contain the value of the file that was submitted.
$filename = $picture_name;

move_uploaded_file($picture, "/path/to/file/$filename");

That will get the file on your system in a directory you choose. You could, at that point fopen the new file, read it in to the database, close the file, and then unlink (delete) the file. Just remember, if you want to read it in to the db, you need to set the fopen to "rb" for reading in binary data. Also you need to insure that the field you are reading the data in to is a binary data field (blob,mediumblob,longblob, etc.) You probably want a mediumblob (this holds up to 16mb of data), a blob only has 64kb, while a longblob can hold up to 4gb!!!
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Post by Russ »

Thanks for your help man, but I wont be uploading to the database now, I think it will be easier if I upload to a folder, I have a code, but I keep getting errors

actually this error

Warning: Unable to open '' for reading: No such file or directory in /home/gmflp/public_html/test.php on line 29
error: upload failed


heres my code

Code: Select all

<?php 
$db = @mysql_connect("$dbhost", "$dbuname", "$dbpage");
if (!$db) &#123;
  echo( "<p>Unable to connect to the " .
        "database server at this time.</p>" );
  exit();
&#125;
mysql_select_db("$dbname", $db);
if (! @mysql_select_db("$dbname") ) &#123;
  echo( "<p>Unable to locate the album " .
        "database at this time.</p>" );
  exit();
&#125;

$maxsize = "200000"; 
$path = $_SERVER&#1111;'DOCUMENT_ROOT'] . "/home/gmflp/public_html/images/albumpics/" . $_POST&#1111;'picname'];


if($submit)

&#123; 
if($uploadedFile != "" && $uploadedFile != "none") 
&#123; 
copy($_FILE&#1111;'uploadedFile'], $path); 
if(file_exists($path)) 
&#123; 
print("upload successful for $picname<br>n"); 
&#125; 
else 
&#123; 
print("error: upload failed<br>n"); 
&#125; 
&#125; 


 


$sql = "INSERT INTO albumdb (name, email, bandname, albumname, picname, review, rating) VALUES ('$name', '$email', '$bandname', '$albumname', '$picname', '$review', '$rating')";
$result = mysql_query($sql); 
echo( "Thank you! Information entered. Click <a href=/album.php>here</a> to view your album review" ); 

&#125; else &#123; 
 
echo "<form method="post" action="$PHP_SELF" enctype="multipart/form-data">"; 
echo "Name:";
echo "<br><input type="Text" name="name">"; 
echo "<br>Email:";
echo "<br><input type="Text" name="email">";
echo "<br>Band Name:";
echo "<br><input type="Text" name="bandname">";
echo "<br>Album Name:";
echo "<br><input type="Text" name="albumname">";
echo "<br>Album Pic:";
echo "<input type="hidden" name="MAX_FILE_SIZE" value="$maxsize">";
echo  "<br><input type="file" name="uploadedFile" size="25">";
echo "<br>Pic Name:";
echo "<br><input type="text" name="picname" value="example.jpg">";
echo "<br>Review:";
echo "<br><textarea name="review" cols=40 rows=8></textarea>";
echo "<br>Rating:";
echo "<br><select name="rating">";
echo "<option value="1">1</option>";
echo "<option value="12">1&#189</option>";
echo "<option value="2">2</option>";
echo "<option value="22">2&#189</option>";
echo "<option value="3">3</option>";
echo "<option value="32">3&#189</option>";
echo "<option value="4">4</option>";
echo "<option value="42">4&#189</option>";
echo "<option value="5">5</option>";
echo "</select>";  
echo "<br>";
echo "<input type="Submit" name="submit" value="Submit"></form>";
 
&#125; 
?>
horobey
Forum Commoner
Posts: 52
Joined: Thu Jul 25, 2002 5:01 pm
Location: Ukraine
Contact:

Post by horobey »

try
echo(">$path<");
copy($_FILES['uploadedFile']['tmp_name'], $path);
echo is needed to make sure that your $path is valid.
Btw from your code:
$path = $_SERVER['DOCUMENT_ROOT'] . "/home/gmflp/public_html/images/albumpics/" . $_POST['picname'];

seems that if user will enter some wrong data in field 'picname' copy will fail. You better make some code to create some unique file name. maybe based on original user's file which you can get:
$users_file_name = $_FILES['uploadedFile']['name'];
now you can add some part to make it unique (use time functions);

Hope this helps
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Post by Russ »

Okay, Now I get this error

>/home/gmflp/public_html/images/albumpics/<
Warning: Unable to create '/home/gmflp/public_html/images/albumpics/': Is a directory in /home/gmflp/public_html/yepyep/album.php on line 41
upload successful for example.jpg


it says it sucessful but it isn't
horobey
Forum Commoner
Posts: 52
Joined: Thu Jul 25, 2002 5:01 pm
Location: Ukraine
Contact:

Post by horobey »

Look @ string where you assign value to $path:
$path = $_SERVER['DOCUMENT_ROOT'] . "/home/gmflp/public_html/images/albumpics/" . $_POST['picname'];

And now to error you've got. Seems that $_POST['picname'] part is nod added to path at all. In other words field picname is empty. I would say dont use it at all.
$my_unique_name = "00001.jpg";// << just example use functions like time(); uniqid(); to generate unique name.

$path = $_SERVER['DOCUMENT_ROOT'] . "/home/gmflp/public_html/images/albumpics/$my_unique_name";

never relay on user's input.
{good user -- dead user}
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Post by Russ »

oh no, the picname shouldn't even be there I don't think, that just goes into the database just so that when I show the review, it links up to the pic right.
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Post by Russ »

okay this is wierd, now I get this

home/gmflp/public_html/images/albumpics/
Warning: Unable to create '/home/gmflp/public_html/images/albumpics/': Is a directory in /home/gmflp/public_html/yepyep/album.php on line 41
upload successful for pic2.jpg

it says that it was uploaded, but when I check, it hasn't been, I chnaged the code a little too

Code: Select all

$path = "/home/gmflp/public_html/images/albumpics/"; 


if($submit)

&#123; 
if($uploadedFile != "" && $uploadedFile != "none") 
&#123;
echo("$path"); 
copy($_FILES&#1111;'uploadedFile']&#1111;'tmp_name'], $path);  
if(file_exists($path)) 
&#123; 
print("upload successful for $picname<br>n"); 
&#125; 
else 
&#123; 
print("error: upload failed<br>n"); 
&#125; 
&#125;
horobey
Forum Commoner
Posts: 52
Joined: Thu Jul 25, 2002 5:01 pm
Location: Ukraine
Contact:

Post by horobey »

it sayz that it was uploaded coz of err in ur code:
if(file_exists($path)){} will return "true" for sure. Because $path contains legal path for ur system. But this is path to directory (folder) not path to file. So copy failed and u have no file stored on ur system. Use this

if(copy( )){
//ok we got it
}else{
//<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>, we dont
}

and read my prev post aabout path carefuly. U must add e file name to $path. Or copy will fail.

BTW do you live somewhere near Swarthmore, Pennsylvania ?
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

i use this for my upload script, written for PHP 4.0.x so you'll have to change the $picfile values to $_FILES[] value...neway:

Code: Select all

<?php
if ($upload && $picfile <> "none" && $picfile <> "") &#123;
$error = 0;
$image_width=595;
$maxsize = 300000;

$registered_types = array(
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg"
);

$allowed_types = array("image/pjpeg", "image/gif");
$log = "<b>Errors for "$picfile_name":</b><ul>";

if (!in_array($picfile_type,$allowed_types)) &#123;
        $error = 1;
        $log .=  "<li>Image type must be jpg or gif</li>";
&#125;

if ($picfile_size > $maxsize) &#123;
        $error = 1;
        $log .= "<li>Image size ($source_size) is greater than allowed size ($maxsize)</li>";
&#125;

$size = GetImageSize($picfile);
list($foo,$width,$bar,$height) = explode(""",$size&#1111;3]);

if ($width > $image_width) &#123;
        $error = 1;
        $log .= "<li>Your image width should be less than $image_width Pixels</li>";
&#125;

$log .= "</ul>";

$picfile_name=ereg_replace("'", "", $picfile_name);
$picfile_name=stripslashes($picfile_name);
if ($error == 0) &#123;
      @copy($picfile, "path/to/folder/$picfile_name");
     echo "File Uploaded";
&#125; else echo $log;

unset($upload);

&#125;


?>
horobey
Forum Commoner
Posts: 52
Joined: Thu Jul 25, 2002 5:01 pm
Location: Ukraine
Contact:

Post by horobey »

I have just forgotten that I have a script which handles image upload and resize.
Please visit:
http://horobey.jlxlinux.com/
In left side menu choose: demos>php/mysql>imageresizer
From the right side page you will be able to download zip with sources
also you can test script online.
It is not perfect because I did it in some half an hour just as an
experiment but in course of time I noticed that lots of people are
downloading it, so I made a little improved version.
Please dont upload to big files while testing it online from my site.
Please sign my guestbook
Russ
Forum Newbie
Posts: 21
Joined: Tue Jun 25, 2002 3:33 pm

Post by Russ »

okay, I finally got the bad boy to work, thanks guys for all your help. FYI heres the code

Code: Select all

$maxsize = "200000"; 
$path = "/home/gmflp/public_html/images/albumpics/$picname"; 


if($submit)

&#123; 
if($uploadedFile != "" && $uploadedFile != "none") 
&#123; 
copy($_FILES&#1111;'uploadedFile']&#1111;'tmp_name'], $path); 
if(file_exists($path))
&#123; 
print ("upload successful for $picname<br>"); 
&#125; 
else 
&#123; 
print ("error: upload failed<br>n"); 
&#125; 
&#125;
Post Reply