Page 1 of 1

modify script?

Posted: Fri Sep 02, 2005 9:56 pm
by techker
hey guys i was woundering if sombody could help me modify my script.

it is a simple upload script but i would need it to overide files.

when i upload the same file i need it to overide the old one in that directory.

now when i upload it says canot upload same file.

nice place by the way.going to chat alot here...

foud it by google. :D

Posted: Fri Sep 02, 2005 10:04 pm
by feyd
post your code (remember to use

Code: Select all

tags, read first link in my signature if you're confused by what they are)

Posted: Fri Sep 02, 2005 10:10 pm
by s.dot
hmm, generally...

Code: Select all

$filename = $_FILES['uploadedfile']['name'];

if(is_file('/some/dir/with/pictures/$filename'))
{
    unlink('/some/dir/with/picture/$filename');
}
// continue uploading original file
Although, if you give the original file write access, I can't see why it wouldn't overwrite. In that case, look into chmod()

Posted: Sat Sep 03, 2005 6:21 am
by techker
im brain dead i forgot to put the script.here it is

Code: Select all

// this is the upload dir where files will go.
//Don't remove the /
//Chmod it (777)
$upload_dir = "includes/";   //change to whatever you want.

             //51200 bytes = 50KB
$size_bytes = 111111; //File Size in bytes (change this value to fit your need)

$extlimit = "yes"; //Do you want to limit the extensions of files uploaded (yes/no)
$limitedext = array(".gif",".jpg",".png",".jpeg",".txt"); //Extensions you want files uploaded limited to. also you can use:  //array(".gif",".jpg",".jpeg",".png",".txt",".nfo",".doc",".rtf",".htm",".dmg",".zip",".rar",".gz",".exe");

          //check if the directory exists 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)");
          }
          
  if($uploadform) // if you clicked the (Upload File) button. "If you submitted the form" then upload the file.
  {//begin of if($uploadform).


              //check if no file selected.
              if (!is_uploaded_file($_FILES['filetoupload']['tmp_name']))
              {
                     echo "Error: Please select a file to upload!. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                     exit(); //exit the script and don't do anything else.
              }

              //Get the Size of the File
              $size = $_FILES['filetoupload']['size'];
              //Make sure that file size is correct
              if ($size > $size_bytes)
              {
                    $kb = $size_bytes / 1024;
                    echo "File Too Large. File must be <b>$kb</b> KB. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                    exit();
              }

              //check file extension
              $ext = strrchr($_FILES['filetoupload'][name],'.');
              if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
                    echo("Wrong file extension. ");
                    exit();
              }

              // $filename will hold the value of the file name submetted from the form.
              $filename =  $_FILES['filetoupload']['name'];
              // Check if file is Already EXISTS.
              if(file_exists($upload_dir.$filename)){
                    echo "Oops! The file named <b>$filename </b>already exists. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                    exit();
              }

              //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.
                  echo "File (<a href=$upload_dir$filename>$filename</a>) uploaded! <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                  exit();

              }
                  // print error if there was a problem moving file.
                  else
              {
                  //Print error msg.
                  echo "There was a problem moving your file. <br>»<a href=\"$_SERVER[PHP_SELF]\">back</a>";
                  exit();
              }



  }//end of if($uploadform).

#---------------------------------------------------------------------------------#
// If the form has not been submitted, display it!
else
  {//begin of else
  
      ?>
      <br>
      <h3>::Browse a File to Upload:</h3>
      <i>- Allowed Extensions:</i>

        <b>
      	<?
        // print the file extensions
        for($i=0;$i<count($limitedext);$i++)
	{
		if (($i<>count($limitedext)-1))$commas=", ";else $commas="";
		list($key,$value)=each($limitedext);
		echo $value.$commas;
	}
	?>
	</b>
	
      <br>
      <i>- Max File Size</i> = <b><?echo $size_bytes / 1024; ?> KB </b>
      <br>
      <form method="post" enctype="multipart/form-data" action="<?php echo $PHP_SELF ?>">
      <br>
      <input type="file" name="filetoupload"><br>
      <input type="hidden" name="MAX_FILE_SIZE" value="<?echo $size_bytes; ?>">
      <br>
      <input type="Submit" name="uploadform" value="Upload File">
      </form>

      <?
  
  }//end of else

/*______________________________________________________________________________*/
//   Here is the most interesting part.
//    it views the directory contents.....i'll disscuss next version. (ver 2.0)
echo "<br><br><hr><br><b>Current Uploaded Files:</b><br>";
?>
<table border="1" bordercolor="#C0C0C0" cellspacing="1">
	<tr>
		<td bgcolor="#F4F4F4">File Name</td>
		<td bgcolor="#F4F4F4">File Size</td>
	</tr>

<?

$rep=opendir($upload_dir);
while ($file = readdir($rep)) {
	if($file != '..' && $file !='.' && $file !=''){
		if (!is_dir($file)){

                        // print the file name and then make a link.
                        echo "<tr><td> <a href=\"$upload_dir$file\" target=_blank>$file</a> </td>";
                        
                        #------------begin of file size.
    	                //print the file size.
    	                $file_size = filesize($upload_dir."".$file);

		        if ($file_size >= 1048576)
		        {
			$show_filesize = number_format(($file_size / 1048576),2) . " MB";
		        }
		        elseif ($file_size >= 1024)
		        {
			$show_filesize = number_format(($file_size / 1024),2) . " KB";
		        }
		        elseif ($file_size >= 0)
		        {
			$show_filesize = $file_size . " bytes";
		        }
		        else
		        {
			$show_filesize = "0 bytes";
		        }

		        echo "<td> $show_filesize </td></tr>";
                        #------------end of file size.

                }
	}
}
closedir($rep);
clearstatcache();

?>
</table>
<p align=right>
<br>Script by: <a href=http://www.maaking.com>maaking.com</a>
</p>

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Sat Sep 03, 2005 8:29 am
by feyd
remove the if involving: file_exists($upload_dir.$filename)

:?

Posted: Sat Sep 03, 2005 8:41 am
by techker
nice thx i didn't even notice that line....