teammist Posted March 16, 2013 Share Posted March 16, 2013 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/ Share on other sites More sharing options...
jazzman1 Posted March 16, 2013 Share Posted March 16, 2013 Before to submit the form or after? Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1418997 Share on other sites More sharing options...
teammist Posted March 16, 2013 Author Share Posted March 16, 2013 Before to submit the form or after? When they hit "Upload" the filename changes. So after. Or displaying an error if the file already exists, that would be ok too. Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1418998 Share on other sites More sharing options...
jazzman1 Posted March 16, 2013 Share Posted March 16, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1418999 Share on other sites More sharing options...
teammist Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) 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 March 16, 2013 by teammist Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419001 Share on other sites More sharing options...
jazzman1 Posted March 16, 2013 Share Posted March 16, 2013 What is that? $fn = 'upl/wildcard' Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419005 Share on other sites More sharing options...
teammist Posted March 16, 2013 Author Share Posted March 16, 2013 What is that? $fn = 'upl/wildcard' Ignore the wildcard thing, $fn is short for $filename = 'upl/' (upl is the directory and it needs to find whats in the directory) Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419006 Share on other sites More sharing options...
jazzman1 Posted March 16, 2013 Share Posted March 16, 2013 upl/ This is a directory not a file. Do you know what is different between a file and directory? Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419007 Share on other sites More sharing options...
teammist Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) 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 March 16, 2013 by teammist Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419008 Share on other sites More sharing options...
teammist Posted March 16, 2013 Author Share Posted March 16, 2013 Or could i have it so it changes the name upon upload? Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419012 Share on other sites More sharing options...
jazzman1 Posted March 16, 2013 Share Posted March 16, 2013 (edited) 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 March 16, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/275738-preventing-someone-overwriting-file/#findComment-1419015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.