10-25-2007, 08:01 PM
|
#3 (permalink)
|
|
The Visitor
Join Date: Oct 2007
Posts: 4
Thanks: 0
|
I tried what you were saying. I might not be doing it right, but its just overwritting the ones that are already upload.
PHP Code:
<?php
$upload_dir = "images/"; $size_bytes = 524880; // = 5 megabytes(MB) $limit_file_type = "yes"; $allowed_file_type = array('image/gif', 'image/pjpeg', 'image/jpeg', 'image/png', 'image/jpg');
//check if the directory exist or not. if (!is_dir("$upload_dir")) { die ("The directory <b>($upload_dir)</b> doesn't exist"); } //check if the directory is writable. if (!is_writeable("$upload_dir")){ die ("The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)"); }
//Check first if a file has been selected //is_uploaded_file('filename') returns true if //a file was uploaded via HTTP POST. Returns false otherwise. if (is_uploaded_file($_FILES['filetoupload']['tmp_name'])) {//begin of is_uploaded_file
//Get the Size of the File $size = $_FILES['filetoupload']['size']; //Make sure that $size is less than 1MB (1000000 bytes) if ($size > $size_bytes) { echo "File Too Large. File must be <b>$size_bytes</b> bytes."; exit(); } //check file type if (($limit_file_type == "yes") && (!in_array($_FILES['filetoupload']['type'],$allowed_file_type))) { echo"wrong file type"; exit(); }
// $filename will hold the value of the file name submetted from the form. $filename = $_FILES['filetoupload']['name']; // Check if file is Already EXISTS.
define('DIRECTORY', './images/');
function getImageName($szImage) { $iIndex = 2; if(!file_exists(DIRECTORY . $szImage)) { return $szImage; } $aParts = explode('.', $szImage); if(count($aParts) <= 1) { return $szImage; } $szExt = end($aParts); $iCount = count($aParts); $aParts[$iCount - 1] = $iIndex; $aParts[$iCount] = $szExt; do { $aParts[count($aParts) - 2] = $iIndex++; } while(file_exists(DIRECTORY . implode('.', $aParts))); return implode('.', $aParts); }
//Move the File to the Directory of your choice //move_uploaded_file('filename','destination') Moves afile to a new location. if (move_uploaded_file($_FILES['filetoupload']['tmp_name'],$upload_dir.$filename)) {
//tell the user that the file has been uploaded and make him alink too;). echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded!"; exit();
} else { //Print error echo "There was a problem moving your file"; exit(); } }//end of is_uploaded_file
?>
|
|
|
|