File Upload

He has: 698 posts

Joined: Jul 2005

I need help with two things dealing with PHP uploading. I have a script that uploads files and it works fine, but I need to be able to do a couple things.

First of all, I need it to change the file name. It needs to be changed to something random so that there isn't the possibility of two files with the same name.

Secondly, I need to be able to add the URL to the image into a database.

This is for WWWArt. What will happen is members will fill out the form with the title and type of artwork it is, and then upload a picture of it, and the title and type, along with the URL to the image, needs to be added to a database so that it can be called up when users are browsing the gallery.

Thanks ahead of time for the help. Smiling

Kurtis

demonhale's picture

He has: 3,278 posts

Joined: May 2005

it would be something like this in php

case "image/jpeg":
for(;; ) { $nr++; if (!file_exists($upload_image_folder."/image".$nr.".jpg")) break; }
$filename = "image".$nr.".jpg";
break;

$nr would increment numbers so the saved file will have same names but different numbers like, image1.jpg, image2.jpg and so on

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

...why are you using an empty for() ? Is it even needed if it's empty?

demonhale's picture

He has: 3,278 posts

Joined: May 2005

its an example dude! you can put anything you want there...

Busy's picture

He has: 6,151 posts

Joined: May 2001

If the file is being uploaded you'd only want the file name saved, not the whole URL

If you wanted to keep track of who belongs to what and how many they have ... you could use members id infront of the image name ie: 123_imagename.jpg or even 123_12_imagename.jpg (the 12 would be how many images they have uploaded), all depends on how your system is set up and what your future plans are - always try plan ahead.

You could also use MD5() for total random and shorten the results to how many you wanted.

He has: 698 posts

Joined: Jul 2005

Okay, thanks for the help. I'm going to work on this later on and figure out how I can do the database stuff. Thanks again. Laughing out loud

He has: 698 posts

Joined: Jul 2005

Okay, I'm confused. Where exactly would I put the code (that demonhale provided) in my current file?

Here's what I have:

<?
$MOVE_TO_PATH = '/home/graphicx/public_html/rating_system/images/';


// The following values are used to verify_uploaded_file()

// as the sizes and types that can be uploaded.

$UPLOAD_TYPES['JPG'] = 1; // Allow .jpg files
$UPLOAD_TYPES['JPEG'] = 2; // Allow .jpeg files
$UPLOAD_TYPES['GIF'] = 1; // Allow .gif files
$UPLOAD_TYPES['PNG'] = 1; // Allow .png files

$UPLOAD_SIZES['max'] = 1000000; // Make sure the file is under 1MB

$UPLOAD_SIZES['min'] = 0; // in size




echo 'File: ' . $HTTP_POST_FILES['upload_file']['name'] . '
' .

'Size: ' . $HTTP_POST_FILES['upload_file']['size'] . '

';




// Verify the size and type of the file

$intResult = verify_uploaded_file(

$HTTP_POST_FILES['upload_file']['name'],
$HTTP_POST_FILES['upload_file']['size']);




// The file is not acceptable

if ($intResult != 1)

{

$msg_base = $HTTP_POST_FILES['upload_file']['name'] .

' is unacceptable.

';




//die() and display error message

if ($intResult == -1)

{

die($msg_base . 'Reason: File size is too larger.');

}

elseif ($intResult == -2)

{

die($msg_base . 'Reason: File type is not allowed.');

}

}


// The file has been accepted; verify it and move it to the server 

if (! move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], $MOVE_TO_PATH .

$HTTP_POST_FILES['upload_file']['name']) )

{

die('You did not upload a file or the file could not be moved to ' .

$MOVE_TO_PATH . $HTTP_POST_FILES['upload_file']['name']);

}

else

{

echo $HTTP_POST_FILES['upload_file']['name']

. ' was uploaded successfully.<br />';

}




function verify_uploaded_file($strName, $intSize)

/* $strName and $intSize are taken from

the uploaded file's information.
Also, make sure that the global variable $UPLOAD_SIZES

and $UPLOAD_TYPES are defined prior to calling

this function. */

{

// Check file size

if ($intSize < $GLOBALS['UPLOAD_SIZES']['min'] ||

$intSize > $GLOBALS['UPLOAD_SIZES']['max'])

{

return -1;

}




// Check file type

$arrSegments = split('[.]', $strName); // may contain multiple dots

$strExtension = $arrSegments[count($arrSegments) - 1];




if ($GLOBALS['UPLOAD_TYPES'][strtoupper($strExtension)] != 1)

{

return -2; // File type not defined/allowed

}


// All tests have passed; this file is valid

return 1;

}
?>
'

Kurtis

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.