Page 1 of 1
Image Help
Posted: Tue Jul 23, 2002 1:08 pm
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??
file uploads, images, etc.
Posted: Tue Jul 23, 2002 3:02 pm
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!!!
Posted: Thu Jul 25, 2002 3:20 pm
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) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
mysql_select_db("$dbname", $db);
if (! @mysql_select_db("$dbname") ) {
echo( "<p>Unable to locate the album " .
"database at this time.</p>" );
exit();
}
$maxsize = "200000";
$path = $_SERVERї'DOCUMENT_ROOT'] . "/home/gmflp/public_html/images/albumpics/" . $_POSTї'picname'];
if($submit)
{
if($uploadedFile != "" && $uploadedFile != "none")
{
copy($_FILEї'uploadedFile'], $path);
if(file_exists($path))
{
print("upload successful for $picname<br>n");
}
else
{
print("error: upload failed<br>n");
}
}
$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" );
} else {
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½</option>";
echo "<option value="2">2</option>";
echo "<option value="22">2½</option>";
echo "<option value="3">3</option>";
echo "<option value="32">3½</option>";
echo "<option value="4">4</option>";
echo "<option value="42">4½</option>";
echo "<option value="5">5</option>";
echo "</select>";
echo "<br>";
echo "<input type="Submit" name="submit" value="Submit"></form>";
}
?>
Posted: Thu Jul 25, 2002 5:01 pm
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
Posted: Thu Jul 25, 2002 5:09 pm
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
Posted: Fri Jul 26, 2002 1:22 am
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}
Posted: Fri Jul 26, 2002 5:27 pm
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.
Posted: Fri Jul 26, 2002 5:42 pm
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)
{
if($uploadedFile != "" && $uploadedFile != "none")
{
echo("$path");
copy($_FILESї'uploadedFile']ї'tmp_name'], $path);
if(file_exists($path))
{
print("upload successful for $picname<br>n");
}
else
{
print("error: upload failed<br>n");
}
}
Posted: Sat Jul 27, 2002 12:46 am
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'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 ?
Posted: Sat Jul 27, 2002 12:57 am
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 <> "") {
$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)) {
$error = 1;
$log .= "<li>Image type must be jpg or gif</li>";
}
if ($picfile_size > $maxsize) {
$error = 1;
$log .= "<li>Image size ($source_size) is greater than allowed size ($maxsize)</li>";
}
$size = GetImageSize($picfile);
list($foo,$width,$bar,$height) = explode(""",$sizeї3]);
if ($width > $image_width) {
$error = 1;
$log .= "<li>Your image width should be less than $image_width Pixels</li>";
}
$log .= "</ul>";
$picfile_name=ereg_replace("'", "", $picfile_name);
$picfile_name=stripslashes($picfile_name);
if ($error == 0) {
@copy($picfile, "path/to/folder/$picfile_name");
echo "File Uploaded";
} else echo $log;
unset($upload);
}
?>
Posted: Sat Jul 27, 2002 1:30 am
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
Posted: Sat Jul 27, 2002 7:47 am
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)
{
if($uploadedFile != "" && $uploadedFile != "none")
{
copy($_FILESї'uploadedFile']ї'tmp_name'], $path);
if(file_exists($path))
{
print ("upload successful for $picname<br>");
}
else
{
print ("error: upload failed<br>n");
}
}