Jump to content

Preventing Someone Overwriting File


teammist

Recommended Posts

 

<?php





$uploadpath = 'upl/';      // directory to store the uploaded files

$max_size = 10000;          // maximum file size, in KiloBytes

$alwidth = 5000;            // maximum allowed width, in pixels

$alheight = 5000;           // maximum allowed height, in pixels

$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');       

if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {

  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name

  $sepext = explode('.', strtolower($_FILES['fileup']['name']));

  $type = end($sepext);       // gets extension

  list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height

  $err = '';         // to store the errors



  // Checks if the file has allowed type, size, width and height (for images)

  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';

  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';

  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;



  // If no errors, upload the image, else, output the errors

  if($err == '') {

    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) {

      echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';

      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';

      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';

      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;

      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';

    }

    else echo '<b>Unable to upload the file.</b>';

  }

  else echo $err;

}

?>





<div style="margin:1em auto; width:333px; text-align:center;">

 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">

  Upload Image: <input type="file" name="fileup" /><br/>

  <input type="submit" name='submit' value="Upload" />

 </form>

</div>

 

How can i prevent someone uploading a file with the same name? Or Randomize the name like imgur does?

Link to comment
Share on other sites

http://php.net/manual/en/function.file-exists.php

 

You can run a javascript code and using an AjaxRequest to check if that particular file exist in web server directory.

 

If the file exist return message, if doesn't exist proceed the server script in php and make the same if test but this time on the server side. 

Link to comment
Share on other sites

http://php.net/manual/en/function.file-exists.php

 

You can run a javascript code and using an AjaxRequest to check if that particular file exist in web server directory.

 

If the file exist return message, if doesn't exist proceed the server script in php and make the same if test but this time on the server side. 

That wont work.... Or how do i do do a wilfcard on the file detection?  upl\wildcard.wildcard ?

 

 

  $fn = 'upl/wildcard';

 

also

  if (file_exists($fn)) $err .= '<br/>The File Name You Are trying to upload already exists<br> Please rename the file'; // This is correct right?

 

 

 

 

EDIT: THAT wont work either... I just realised........ Its saying it already exists every time.... ><

Edited by teammist
Link to comment
Share on other sites

 

upl/

 

This is a directory not a file. Do you know what is different between a file and directory?

Yes......... <> $filename '/path/to/foo.txt'; << PATH to....

 

Basically, the link you supplied me with was not what i needed? I need something that checks if the file already exists and will cause an error if it already exists... I dont want to have to state which file, I need it to be ANY file on the system what someone uploads, not just one specific file...

Edited by teammist
Link to comment
Share on other sites

Try,

 

 

<?php
 
$uploadpath = 'upl/';      // directory to store the uploaded files
$max_size = 10000;          // maximum file size, in KiloBytes
$alwidth = 5000;            // maximum allowed width, in pixels
$alheight = 5000;           // maximum allowed height, in pixels
$allowtype = array('bmp', 'gif', 'jpg', 'jpe', 'png');       
if(isset($_FILES['fileup']) && strlen($_FILES['fileup']['name']) > 1) {
  $uploadpath = $uploadpath . basename( $_FILES['fileup']['name']);       // gets the file name
  $sepext = explode('.', strtolower($_FILES['fileup']['name']));

$type = end($sepext);      // gets extension
list($width, $height) = getimagesize($_FILES['fileup']['tmp_name']);     // gets image width and height

  $err = '';         // to store the errors

  // Checks if the file has allowed type, size, width and height (for images)

  if(!in_array($type, $allowtype)) $err .= 'The file: <b>'. $_FILES['fileup']['name']. '</b> not has the allowed extension type.';

  if($_FILES['fileup']['size'] > $max_size*1000) $err .= '<br/>Maximum file size must be: '. $max_size. ' KB.';

  if(isset($width) && isset($height) && ($width >= $alwidth || $height >= $alheight)) $err .= '<br/>The maximum Width x Height must be: '. $alwidth. ' x '. $alheight;

  // If no errors, upload the image, else, output the errors

  if($err == '') {

    // this line checks if the file exist

    return (!file_exists($uploadpath.'.'.$type)) ? true : false;

    if(move_uploaded_file($_FILES['fileup']['tmp_name'], $uploadpath)) {



      echo 'File: <b>'. basename( $_FILES['fileup']['name']). '</b> successfully uploaded:';



      echo '<br/>File type: <b>'. $_FILES['fileup']['type'] .'</b>';



      echo '<br />Size: <b>'. number_format($_FILES['fileup']['size']/1024, 3, '.', '') .'</b> KB';



      if(isset($width) && isset($height)) echo '<br/>Image Width x Height: '. $width. ' x '. $height;



      echo '<br/><br/>Image address: <b>http://'.$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), '\\/').'/'.$uploadpath.'</b>';



    }



    else echo '<b>Unable to upload the file.</b>';



  }



  else echo $err;



}



?>





<div style="margin:1em auto; width:333px; text-align:center;">



 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">



  Upload Image: <input type="file" name="fileup" /><br/>



  <input type="submit" name='submit' value="Upload" />



 </form>



</div>
Edited by jazzman1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.