-
-
Save zvineyard/3530917 to your computer and use it in GitHub Desktop.
<form action="" enctype="multipart/form-data" method="post"> | |
<input id="file" name="file" type="file" /> | |
<input id="Submit" name="submit" type="submit" value="Submit" /> | |
</form> |
<?php | |
// Upload and Rename File | |
if (isset($_POST['submit'])) | |
{ | |
$filename = $_FILES["file"]["name"]; | |
$file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention | |
$file_ext = substr($filename, strripos($filename, '.')); // get file name | |
$filesize = $_FILES["file"]["size"]; | |
$allowed_file_types = array('.doc','.docx','.rtf','.pdf'); | |
if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000)) | |
{ | |
// Rename file | |
$newfilename = md5($file_basename) . $file_ext; | |
if (file_exists("upload/" . $newfilename)) | |
{ | |
// file already exists error | |
echo "You have already uploaded this file."; | |
} | |
else | |
{ | |
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $newfilename); | |
echo "File uploaded successfully."; | |
} | |
} | |
elseif (empty($file_basename)) | |
{ | |
// file selection error | |
echo "Please select a file to upload."; | |
} | |
elseif ($filesize > 200000) | |
{ | |
// file size error | |
echo "The file you are trying to upload is too large."; | |
} | |
else | |
{ | |
// file type error | |
echo "Only these file typs are allowed for upload: " . implode(', ',$allowed_file_types); | |
unlink($_FILES["file"]["tmp_name"]); | |
} | |
} | |
?> |
Great
goo job
Nice.....
Thank you so much!
Works great..Thank you
Thank you
Hi, Thank you for the code, I have just noticed some little mistakes on the comments:
$file_basename = substr($filename, 0, strripos($filename, '.')); // This returns file name
$file_ext = substr($filename, strripos($filename, '.')); // This returns file ext
good job dude thanks alot
its OK? for 5.3 PHP
thanku
exactly what I needed
Thanks!
Good
cool
hellz yeah
The code works okay.
Kindly I want to rename the uploaded file as per logged in user. Assist please
Thsk for the code!!!!
EliudMathu:You can this?
// Rename file
$file_basename = $_SESSION['username'] ;
$newfilename = $file_basename . $file_ext;
if the user name is Jon, you file is rename for Jon.pdf
firts, you need a Login system Regards,
Have a look at pathinfo()
: http://php.net/manual/en/function.pathinfo.php
It's a lot easier (and less fragile) than calling strpos()
and whatnot.
thanks man!
hello, I would like to use this that after loading any png image with the result after the upload must always be "image.png", I can not
Hi!
Thank you for this code very simple than mine.
One thing, I don't understand how it deal with "$allowed_file_types = array('.doc','.docx','.rtf','.pdf');"
Can you give me some explanations, please?
Thank you
EDIT: Do not matter about my question, I just didn't read carefully your code. Thank you.
good job
Thanks, the code was very helpful to me
dats cool thanks. but can u upload one where the user can read the files from the folder using the browser