Skip to content

Instantly share code, notes, and snippets.

@zvineyard
Created August 30, 2012 15:29
Show Gist options
  • Select an option

  • Save zvineyard/3530917 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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

@evinw
Copy link
Copy Markdown

evinw commented Apr 12, 2015

Great

@yasinyus
Copy link
Copy Markdown

yasinyus commented Sep 1, 2015

goo job

@Musfeq
Copy link
Copy Markdown

Musfeq commented Sep 29, 2015

Nice.....

@helloromero
Copy link
Copy Markdown

Thank you so much!

@canadadealsca
Copy link
Copy Markdown

Works great..Thank you

@alexeychabala
Copy link
Copy Markdown

Thank you

@marko85to
Copy link
Copy Markdown

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
Copy Markdown

mocxzwp commented Jul 10, 2016

good job dude thanks alot

@edson36
Copy link
Copy Markdown

edson36 commented Oct 7, 2016

its OK? for 5.3 PHP

@rekipur
Copy link
Copy Markdown

rekipur commented Oct 13, 2016

@Himanshupiet
Copy link
Copy Markdown

thanku

@pawebgate
Copy link
Copy Markdown

exactly what I needed

@DavidCorral94
Copy link
Copy Markdown

Thanks!

@celestinmunyaneza
Copy link
Copy Markdown

Good

@Ahrorooney
Copy link
Copy Markdown

cool

@Redbot
Copy link
Copy Markdown

Redbot commented Sep 23, 2017

hellz yeah

@EliudMathu
Copy link
Copy Markdown

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

@Exadevelop
Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

thanks man!

@alexfrenk
Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

good job

@sefakor20
Copy link
Copy Markdown

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