NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Edit a PHP upload script

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 10-28-2005, 04:32 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2005
Location: Belfast
Posts: 1,217
Ericsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to behold
 


Save a Life Child Abuse Cystic Fibrosis Third World Education AIDS/HIV Third World Education Save a Life Save a Life Child Abuse

Edit a PHP upload script


Code:
<?
// Upload settings
$folder = "img/files/"; // Folder in which to store files
$maxlimit = 50000; // Set maximum file limit (in bits)
$allowed_ext = "jpg,gif,png,jpeg,bmp,png"; // Set allowed extensions (split using comma)
$overwrite = "no"; // Allow file overwrite? yes/no

$match = ""; // Clear match variable; for security purposes
$filesize = $_FILES['userfile']['size']; // Get file size (in bits)
$filename = strtolower($_FILES['userfile']['name']); // Get file name; make it all lowercase


if(!$filename || $filename==""){ // File not selected
   $error = "- No file selected for upload.<br>";
}elseif(file_exists($folder.$filename) && $overwrite=="no"){ // Check if file exists
   $error = "- File already exists: $filename<br>";
}

// Check if file size
if($filesize < 1){ // File is empty
   $error .= "- File size is empty.<br>";
}elseif($filesize > $maxlimit){ // File is more than maximum
   $error .= "- File size is too big.<br>";
}

$file_ext = preg_split("/\./",$filename); // Split filename at period (name.ext)
$allowed_ext = preg_split("/\,/",$allowed_ext); // Create array of extensions
foreach($allowed_ext as $ext){
   if($ext==$file_ext[1]) $match = "1"; // File is allowed
}

// File extension not allowed
if(!$match){
   $error .= "- File type isn't allowed: $filename<br>";
}

if($error){
   print "Error trying to upload file:<br> $error"; // Display error messages
}else{
   if(move_uploaded_file($_FILES['userfile']['tmp_name'], $folder.$filename)){ // Upload file
      print "Success! The file has been uploaded: Http://www.freephotohost.co.uk/img/files/$filename";
   }else{
      print "Error! File size might exceed upload limit of server. Try again."; // Display error
   }
}

?>
How can I make it so the files names are changed?
Ericsson is offline  
Old 10-28-2005, 04:59 AM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Gave it a quick look over, here's what I ended up with, this should work:

PHP Code:
<?php

//Upload settings

//Folder in which to store files
$folder "img/files/"
????: NamePros.com http://www.namepros.com/code/135613-edit-a-php-upload-script.html

//Set maximum file limit (in bites)
$maxlimit 50000

//Set allowed extensions (split using comma)
$allowed_ext "jpg, gif, png, jpeg, bmp, png";

//Allow file overwrite? yes/no
$overwrite "no"

//Clear match variable; for security purposes
$match ""

//Get file size (in bites)
$filesize $_FILES['userfile']['size'];

//Get file name; make it all lowercase
$filename strtolower($_FILES['userfile']['name']); 

if(!
$filename || $filename == "")
{
//File not selected
  
$error "- No file selected for upload.<br>";
}
elseif(
file_exists($folder.$filename) && $overwrite == "no")
{
//Check if file exists
  
$error "- File already exists: $filename<br>";
}

//Check if file size
if($filesize 1)
//File is empty
   
$error .= "- File size is empty.<br>";
}
elseif(
$filesize $maxlimit)
//File is more than maximum
   
$error .= "- File size is too big.<br>";
}

//Split filename at period (name.ext)
$file_ext explode("."$filename);

// Create array of extensions
$allowed_ext explode(","$allowed_ext);

foreach(
$allowed_ext as $ext)
{
  if(
$ext == $file_ext[1])
  {
    
$match "1"// File is allowed
  
}
}

//Assign a unique/random name
$unique substr(md5(uniqid(rand())), 010);

//File extension not allowed
if(!$match)
{
   
$error .= "- File type isn't allowed: $filename<br>";
}

if(
$error)
{
   print 
"Error trying to upload file:<br> $error"// Display error messages
}
else
{
  if(
move_uploaded_file($_FILES['userfile']['tmp_name'], $folder.$unique.".".$file_ext))
  {
//Upload file
    
print "Success! The file has been uploaded: Http://www.freephotohost.co.uk/img/files/".$unique.".".$file_ext;
????: NamePros.com http://www.namepros.com/showthread.php?t=135613
  }
  else
  {
    print 
"Error! File size might exceed upload limit of server. Try again."// Display error
  
}
 }
 
?>
Eric is offline  
Old 10-28-2005, 02:48 PM THREAD STARTER               #3 (permalink)
Senior Member
Join Date: Aug 2005
Location: Belfast
Posts: 1,217
Ericsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to behold
 


Save a Life Child Abuse Cystic Fibrosis Third World Education AIDS/HIV Third World Education Save a Life Save a Life Child Abuse
Thanks for the help But it doesnt work, says Error trying to upload file:
- File already exists: user.gif
Ericsson is offline  
Old 10-28-2005, 02:58 PM   #4 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Hmm well still waking up so can't get a good look over it, but since you are now randomizing the names, should be able to remove:

PHP Code:
elseif(file_exists($folder.$filename) && $overwrite == "no"
????: NamePros.com http://www.namepros.com/showthread.php?t=135613
{
//Check if file exists 
  
$error "- File already exists: $filename<br>"

Eric is offline  
Old 10-28-2005, 11:34 PM   #5 (permalink)
Eating Pie
 
iNod's Avatar
Join Date: Nov 2004
Location: Canada
Posts: 2,272
iNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud of
 


Special Olympics AIDS/HIV Cystic Fibrosis Save The Children Baby Health Cystic Fibrosis
Originally Posted by SecondVersion
Gave it a quick look over, here's what I ended up with, this should work:

PHP Code:
<?php

//Upload settings

//Folder in which to store files
$folder "img/files/"

//Set maximum file limit (in bites)
$maxlimit 50000

//Set allowed extensions (split using comma)
$allowed_ext "jpg, gif, png, jpeg, bmp, png";

//Allow file overwrite? yes/no
$overwrite "no"

//Clear match variable; for security purposes
????: NamePros.com http://www.namepros.com/showthread.php?t=135613
$match ""

//Get file size (in bites)
$filesize $_FILES['userfile']['size'];

//Get file name; make it all lowercase
$filename strtolower($_FILES['userfile']['name']); 

if(!
$filename || $filename == "")
{
//File not selected
  
$error "- No file selected for upload.<br>";
}
elseif(
file_exists($folder.$filename) && $overwrite == "no")
{
//Check if file exists
  
$error "- File already exists: $filename<br>";
}

//Check if file size
if($filesize 1)
//File is empty
   
$error .= "- File size is empty.<br>";
}
elseif(
$filesize $maxlimit)
//File is more than maximum
   
$error .= "- File size is too big.<br>";
}

//Split filename at period (name.ext)
$file_ext explode("."$filename);

// Create array of extensions
$allowed_ext explode(","$allowed_ext);

foreach(
$allowed_ext as $ext)
{
  if(
$ext == $file_ext[1])
  {
    
$match "1"// File is allowed
  
}
}

//Assign a unique/random name
$unique substr(md5(uniqid(rand())), 010);

//File extension not allowed
if(!$match)
{
   
$error .= "- File type isn't allowed: $filename<br>";
}

if(
$error)
{
   print 
"Error trying to upload file:<br> $error"// Display error messages
}
else
{
  if(
move_uploaded_file($_FILES['userfile']['tmp_name'], $folder.$unique.".".$file_ext))
  {
//Upload file
    
print "Success! The file has been uploaded: Http://www.freephotohost.co.uk/img/files/".$unique.".".$file_ext;
  }
  else
  {
    print 
"Error! File size might exceed upload limit of server. Try again."// Display error
  
}
 }
 
?>
PHP Code:
  if(move_uploaded_file($_FILES['userfile']['tmp_name'], $folder.$unique.".".$file_ext)) 
Should be
????: NamePros.com http://www.namepros.com/showthread.php?t=135613
PHP Code:
  if(move_uploaded_file($_FILES['userfile']['tmp_name'], $folder.$unique.".".$file_ext['1'])) 
I see nothing really wrong with it. I will give it a go over tomorrow.

iNod
__________________
I feel old.
iNod is offline  
Old 10-29-2005, 02:55 AM THREAD STARTER               #6 (permalink)
Senior Member
Join Date: Aug 2005
Location: Belfast
Posts: 1,217
Ericsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to behold
 


Save a Life Child Abuse Cystic Fibrosis Third World Education AIDS/HIV Third World Education Save a Life Save a Life Child Abuse
Thats fixed, thannks
I tried uploading a gif and it said file type not allowed, but its on the allowed list. Ant ideas? It was working before.
Ericsson is offline  
Old 10-31-2005, 10:19 AM   #7 (permalink)
JFS
NamePros Regular
 
JFS's Avatar
Join Date: Oct 2005
Location: Portugal
Posts: 800
JFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to allJFS is a name known to all
 



this should work...

Code:
$extensiones=array("jpeg","jpg","gif");
$num = count($extensiones);
$valor = $num-1;
$bol="n";
for($i=0; $i<=$valor; $i++) {
    if($extensiones[$i] == $var[1]) {
    $bol="y";
    }
}
if ($bol=="n"){
    echo "File type not permited...";
    echo "File types permited are blablablablabla...";
}
if ($bol=="y"){
    //do upload routine mine is 40 lines long ehehe
}
hope it helps...
????: NamePros.com http://www.namepros.com/showthread.php?t=135613

edited:
fixed typo
__________________
Joćo Fernandes Silva
JFS is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 01:09 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger