Safe php upload

Started on the old forum in May 2021 · view the original in the archive · old links redirect here

5 replies · 84 views · started 5 years ago · 1 min read

2 participants
IneedHelp Newbie · 3 posts #1

Hello everyone.

Can someone provide a safe php upload script and a safe .htaccess file to go with it?

GumSlone Administrator Principal · 1,690 posts #2

save the file without extension also use some hash as the file name for disk storage and add only read rights to it, also do not store the file in a public directory, store the real file name in the database.

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

IneedHelp Newbie · 3 posts #3

Thanks!

The hashed name is very good to prevent unwanted files being executed and making them read only will make sure the file can not be changed.

so: [Pre file checks here would also be handy to know the best ones to use.] move_uploaded_file($temp, 'nonPublicDirectory/HashedFileName'); chmod("nonPublicDirectory/HashedFileName", 0644); // should this be 0400 ? (owner only read) mysqli_query($connection, " INSERT INTO database hashed='HashedFileName', real='file.png.php'"); //(file name sanitized)

Then how would you pass the file to the browser without executing it?

GumSlone Administrator Principal · 1,690 posts #4

For file download, see below:

header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header('Content-disposition: attachment; filename="'.$filename.'"');
echo readfile($path_to_file_on_disk);

you can replace application/octet-stream with a proper mime type

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

IneedHelp Newbie · 3 posts #5

Thank you GumSlone.

How would you do the same for images in html

If you had a lot of images in 1 page it would cause high cpu?

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close