uploading script , lil problem.

Associate
Joined
29 May 2005
Posts
144
Hello..

Im trying to create an upload script which will upload a file into a directory and add the 'file name' into a database called 'gallery'.

I have got the upload part working, it uploads an image for example into my specified folder, but the adding to database part I cant get working. I must be doing something stupid.

I tryed to add the $userfile to the database, as thats the name of my file import in the form, but that didnt add anything to the database at all.

Ideally im after the name of the image, so if i was to upload 'mofish.jpg' it would add 'mofish.jpg' to the database.

I cant see where im going wrong, or even if im on the right path. Here is my code anyways, so far. Needs more validation and stuff I know, its basic for now. :p

Thanks, Mofish

Code:
<?php
//theres a warning and i cant get rid of it, this prevents it. :)
error_reporting(0);

 $userfile = $_POST['userfile'];
 $member = $_SESSION['member'];
 
 $query = "insert into `gallery` (`image`, `author`) values ($userfile', '$member')";
 
 $uploaddir = '/home/mo/public_html/php-development/UNISite/pages/upload/';
 $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

 echo "<div>Welcome - You Have A <b>5MB</b> Upload Limit</div>";
	if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   		if (mysql_query($query)){
			echo "Successfully added to database.\n";
		} else {
			echo "not added to database.\n";
		}
		echo "Successfully uploaded.\n";
	} else {
   		echo "An Error Occoured, Please Try Again\n";
	}
?> 

<form enctype="multipart/form-data" action="index.php?page=upload" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
    Send this file: <input name="userfile" type="file" class="loginbox" />
    <input type="submit" value="Send File" class="loginbox" />
</form>
 
Last edited:
I'm guessing im doing something horribly wrong to have no replys :cool: oh dear hehe. Could anyone assist me in making this code work? ;)

This is what im trying to achieve...

1) upload a file to a folder on the server called uploads.
2) add the file name (example: mofish.jpg) to a database field called image
3) add the '$_SESSION['member'];' name for each file (so i can identify who uploaded it) to a database field called author

any help would be greatly appreciated. PS beansprout where you hiding :p

PHP:
<form action="index.php?page=upload" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="200000">
    <input type="file" name="imagefile">
    <input type="submit" name="upload" value="Upload Image">
</form>

 <?php
 error_reporting(0);

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

 // Get the details of "imagefile"
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];
$member = $_SESSION['member'];

//Open the image using the imagecreatefrom..() command based on the MIME type.
switch($mimetype) {
    case "image/jpg":
    case "image/jpeg":
        $i = imagecreatefromjpeg($temporary_name);
        break;
    case "image/gif":
        $i = imagecreatefromgif($temporary_name);
        break;
    case "image/png":
        $i = imagecreatefrompng($temporary_name);
        break;
}

//Delete the uploaded file
unlink($temporary_name);

//Save a copy of the original
imagejpeg($i,"gallery/" . $filename ,80);

//Specify the size of the thumbnail
$dest_x = 150;
$dest_y = 150;
 
//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    //Is the width of the original bigger than the height?
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    //Using the original dimensions
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

//Generate a new image at the size of the thumbnail
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);

//Copy the original image data to it using resampling
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));

//Save the thumbnail
imagejpeg($thumb, "gallery/tn_" . $filename, 80);

echo $filename;

//set query
$query = "insert into `gallery` (`image`, `author`) values ('$filename', '$member')";

if (mysql_query($query)){
	echo "Successfully added to database.\n";
} else {
	echo "not added to database.\n";
} 

}else{
echo "upload a file above ^.^ .\n";
}
?>
MoFish
 
Last edited:
Well I think you want $userfile = $_FILES['userfile']['name'] to add the original name of the file to the DB.

I think the missing ' at the start of the values ($userfile', might also be an issue.
 
Back
Top Bottom