tom100 Posted February 18, 2007 Share Posted February 18, 2007 I see a lot of questions about file uploads, etc, so I figured I would make a script that everyone can use. You guys can tell me if it works and if it helps at all. function: uploadFiles(string $cfile) $cfile is a key in the $_FILES array. Returns: 0 if failed Filedirectory/Filename if success Error messages are stored in a global variable called $_UPLOADERROR Here is the include file: <?php //######################################### //# UPLOAD SCRIPT # //#---------------------------------------# //# Step 1: Change the path constant to # //# the directory you want files saved. # //#---------------------------------------# //# Step 2: Change the CHMOD constant to # //# whatever you want. # //#---------------------------------------# //# Step 3: Setup your form and call this # //# script with the $_FILE key. # //######################################### define("_UPLOADPATH", "file_uploads/"); define("_UPLOADCHMOD", "0755"); define("_UPLOADDIR", "0755"); function uploadFiles($cfile) { $_UPLOADERROR=""; global $_UPLOADERROR; $errorMessages=array(); $errorMessages[0]="Uploaded files must have an extension."; $errorMessages[1]="The file upload directory does not exist."; $errorMessages[2]="The file permissions could not be changed."; $errorMessages[3]="The file could not be moved to the upload directory."; $errorMessages[4]="The uploaded file was invalid or corrupt."; $errorMessages[5]="The uploaded file was invalid."; $errorMessages[6]="The uploaded file size is larger than the size allowed in PHP.ini."; $errorMessages[7]="The uploaded file size is larger than the size allowed in the html form."; $errorMessages[8]="The uploaded file was only partially uploaded."; $errorMessages[9]="No file was uploaded."; $errorMessages[10]="The servers temporary upload directory was not found."; $errorMessages[11]="File could not be written to disk."; $errorMessages[12]="File upload stopped by extension."; $errorMessages[13]="An unknown error has occured."; // Get file information $filename = $_FILES[$cfile]['name']; $filesize = $_FILES[$cfile]['size']; $filetype = $_FILES[$cfile]['type']; $file_tmp = $_FILES[$cfile]['tmp_name']; $file_err = $_FILES[$cfile]['error']; $file_ext = strrchr($filename, '.'); if (($file_err == 0) && ($filesize != 0)) // Check for upload errors { if (!$file_ext) // Check if file has an extension { unlink($file_tmp); $_UPLOADERROR = $errorMessages[0]; return 0; } if (is_uploaded_file($file_tmp)) // Check if the file exists on our server { $dir = _UPLOADPATH; if (!is_dir($dir)) // Check if upload directory exists. { if (!mkdir($dir, _UPLOADDIR)) //If not, try and make it. { unlink($file_tmp); $_UPLOADERROR = $errorMessages[1]; return 0; } } if (file_exists("{$dir}/{$filename}")) { $filen_id=1; $filen=$filename; $file_r=substr($filename, 0, strrpos($filename, ".")); while (true) { if (file_exists("{$dir}/{$filen}")) { $filen=$file_r.$filen_id.$file_ext; // Append a number to the end. $filen_id++; } else { $filename=$filen; // Update file name and continue break; } } } @chmod($dir, _UPLOADDIR); if (@move_uploaded_file($file_tmp, "{$dir}/{$filename}")) // Try and save the file { if (chmod("{$dir}/{$filename}", _UPLOADCHMOD)) // Try and set permissions { return preg_replace("@[/]+@", "/", "{$dir}/{$filename}"); } else { @unlink("{$dir}/{$filename}"); $_UPLOADERROR = $errorMessages[2]; return 0; } } else { @unlink($file_tmp); $_UPLOADERROR = $errorMessages[3]; return 0; } } else { @unlink($file_tmp); $_UPLOADERROR = $errorMessage[4]; } } else { if ($file_tmp != '') { @unlink($file_tmp); } switch ($file_err) { case '0': $_UPLOADERROR = $errorMessge[5]; break; case '1': $_UPLOADERROR = $errorMessage[6]; break; case '2': $_UPLOADERROR = $errorMessage[7]; break; case '3': $_UPLOADERROR = $errorMessage[8]; break; case '4': $_UPLOADERROR = $errorMessage[9]; break; case '6': $_UPLOADERROR = $errorMessage[10]; break; case '7': $_UPLOADERROR = $errorMessage[11]; break; case '8': $_UPLOADERROR = $errorMessage[12]; break; default: $_UPLOADERROR = $errorMessage[13]; break; } return 0; } } ?> Create that file and save it wherever you want. Then, simply include it on the file upload script and run something similar to this: if (isset($_FILES)) { foreach ($_FILES as $key=>$file) { if (!empty($file['name'])) { echo uploadFiles($key); } } } I have also attached a full working demo. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/39070-form-file-upload-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.