Skip to content

Instantly share code, notes, and snippets.

@zvineyard
Created August 30, 2012 15:29
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save zvineyard/3530917 to your computer and use it in GitHub Desktop.
Save zvineyard/3530917 to your computer and use it in GitHub Desktop.
PHP: Upload and Rename File
<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"]);
}
}
?>
@ultrasamad
Copy link

dats cool thanks. but can u upload one where the user can read the files from the folder using the browser

@evinw
Copy link

evinw commented Apr 12, 2015

Great

@yasinyus
Copy link

yasinyus commented Sep 1, 2015

goo job

@Musfeq
Copy link

Musfeq commented Sep 29, 2015

Nice.....

@helloromero
Copy link

Thank you so much!

@canadadealsca
Copy link

Works great..Thank you

@alexeychabala
Copy link

Thank you

@marko85to
Copy link

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

@mocxzwp
Copy link

mocxzwp commented Jul 10, 2016

good job dude thanks alot

@edson36
Copy link

edson36 commented Oct 7, 2016

its OK? for 5.3 PHP

@rekipur
Copy link

rekipur commented Oct 13, 2016

@Himanshupiet
Copy link

thanku

@pawebgate
Copy link

exactly what I needed

@DavidCorral94
Copy link

Thanks!

@celestinmunyaneza
Copy link

Good

@Ahrorooney
Copy link

cool

@Redbot
Copy link

Redbot commented Sep 23, 2017

hellz yeah

@EliudMathu
Copy link

The code works okay.
Kindly I want to rename the uploaded file as per logged in user. Assist please

@Exadevelop
Copy link

Exadevelop commented Feb 15, 2018

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,

@danielsdeboer
Copy link

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.

@blackestwhite
Copy link

thanks man!

@alexfrenk
Copy link

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

@Bk201Freelance
Copy link

Bk201Freelance commented Oct 21, 2018

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.

@subrotoice
Copy link

good job

@sefakor20
Copy link

Thanks, the code was very helpful to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment