how to make a PHP upload script?
I’m looking for a php script that will upload pictures to a directory /images/userimages and can change the names of the uploaded files and get thier extention (gif jpeg etc.)
September 6, 2010 | Filed Under Scripts FAQs
Comments
2 Responses to “how to make a PHP upload script?”
Powered by Yahoo! Answers
Please see either the W3Schools site or the PHP.net site, they offer copy/paste PHP scripts on file uploading.
<?
/********************************
files.class.php
********************************/
class files{
var $Error;
var $goback;
function files(){
}
function file_form($UserInput){
echo "<div align=\"center\"><form method=\"post\" action=\"$_SERVER[PHP_SELF]\" name=\"FileUpload\" enctype=\"multipart/form-data\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" id=\"LoginTable\" bgcolor=\"#000000\" align=\"center\"><tr><td id=\"Upload\"><strong>**Information**</strong><br><i>Max upload file size is 10MB</i><br><br></td></tr><tr><td id=\"Upload\"><div align=\"center\">File to upload: <input type=\"file\" name=\"file_to_upload\"></div></td></tr><tr><td id=\"Upload\"><div align=\"center\">Access: <select name=\"Access\" id=\"select\"><option value=\"0\">Private</option><option value=\"1\">Public</option></select></div></td></tr><tr><td id=\"Upload\"><div align=\"center\">File Comments:</div></td></tr><tr><td colspan=\"2\" id=\"Upload\"><div align=\"center\"><textarea name=\"FileComments\" cols=\"50\" rows=\"10\"></textarea></div></td></tr><tr><td id=\"Upload\" colspan=\"2\"><div align=\"center\"><input type=\"submit\" value=\"Submit\"> <input type=\"reset\" value=\"Reset\"></div></td></tr></table></form></div>";
}
function add_file($UserInput){
require_once(‘static.class.php’);
if(file_exists("upload/" . $_FILES['file_to_upload']['name']))
{
$this->file_form($UserInput);
echo "<div align=\"center\"><font color=\"red\">The file already exists! Delete first!<br>Try again or $this->goback!</font></div>";
$this->Error == true;
exit;
}
else
{
//Add the file
$this->move_temp_file = move_uploaded_file($_FILES['file_to_upload']['tmp_name'], "upload/" . $_FILES['file_to_upload']['name']);
if(!$this->move_temp_file)
{
$this->file_form($UserInput);
echo "<div align=\"center\"><font color=\"red\">There was a problem uploading your file!<br>Try again or $this->goback!<br>Contact support!</font></div>";
$this->Error == true;
exit;
}
else
{
//Add file information to database
//Generate File ID
$this->file_ID = md5($_SESSION['Path'] . "/" . $_FILES['file_to_upload']['name'] . date($_SESSION['UNIX_EP']));
$this->file_path = $_SESSION['Path'] . "/" . $_FILES['file_to_upload']['name'];
$Nfile_name = $_FILES['file_to_upload']['name'];
$Nfile_type = $_FILES['file_to_upload']['type'];
$Nfile_size = $_FILES['file_to_upload']['size'];
$IVal = $_FILES['file_to_upload']['size'];
$file_size = new static_class();
$file_size->StaticFSize($IVal);
$this->read_file_size = $file_size->FDS;
$this->query_string = "INSERT INTO files (id, UID, File_Name, File_ID, File_Type, File_Path, File_Access, File_DTG, File_Size, Read_File_Size, File_Count, Comments) VALUES(’0′,’$_SESSION[UID]‘, ‘$Nfile_name’,'$this->file_ID’,'$Nfile_type’,'$this->file_path’,'$UserInput[Access]‘,’" . date($_SESSION['DTG']) . "’,'$Nfile_size’,'$this->read_file_size’,’0′,’" . addslashes($UserInput['FileComments']) . "’)";
$this->query = mysql_query($this->query_string);
if(!$this->query)
{
$this->delete_file = unlink("upload/" . $_FILES['file_to_upload']['name']);
$this->file_form($UserInput);
$space_left_message = ($this->space_left <= "0") ? "You do not have any space left" : "Space left:" . $file_size->FDS;
echo "<div align=\"center\"><font color=\"red\">Error adding file to database! No file uploaded! $this->goback</font></div>";
$this->Error == true;
exit;
}
else
{
$this->file_form($UserInput);
echo "<div align=\"center\"><font color=\"red\">File upload complete!</font></div>";
$this->Error == false;
}
}
}
}
function validate_file($UserInput){
require_once(‘static.class.php’);
$this->goback = "<a href=javascript:history.go(-1)><font color=red size=-2>(Go Back)</font></a>";
$this->max_upload_size=pow(1024,2)*10;
//Calculate the file size
$file_size = new static_class();
$IVal = $_FILES['file_to_upload']['size'];
$file_size->StaticFSize($IVal);
$UserInput['file_size'] = $file_size->FDS;
//Check to see if user has enough space left
$this->max_disk_space = pow(1024,2)*10;
//Add all file s