PHP Doku:: Verschiebt eine upgeloadete Datei an einen neuen Ort - function.move-uploaded-file.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDateisystemrelevante ErweiterungenDateisystemDateisystem-Funktionenmove_uploaded_file

Ein Service von Reinhard Neidl - Webprogrammierung.





(PHP 4 >= 4.0.3, PHP 5)

move_uploaded_fileVerschiebt eine upgeloadete Datei an einen neuen Ort


bool move_uploaded_file ( string $filename , string $destination )

Diese Funktion prüft, ob die mit filename bezeichnete Datei eine gültige Upload-Datei ist (d.h., dass sie mittels PHP's HTTP POST Upload-Mechanismus upgeloaded wurde). Ist die Datei gültig, wird sie zum in destination bezeichneten Dateinamen verschoben.

Wenn filename keine gültige Datei ist, wird keine Aktion ausgeführt, und move_uploaded_file() gibt FALSE zurück.

Wenn filename eine gültige Datei ist, jedoch aus irgendeinem Grund nicht verschoben werden kann, wird keine Aktion ausgeführt, und move_uploaded_file() gibt FALSE zurück. Zusätzlich wird eine Warnung ausgegeben.

Diese Prüfung ist speziell dort interessant, wo irgendeine Aktion mit upgeloadeten Dateien deren Inhalt dem User zugänglich machen könnte (oder auch anderen Usern auf dem selben System).

Hinweis: Wenn Safe Mode aktiviert ist, überprüft PHP, ob die Dateien/Verzeichnisse, die mit dem Skript bearbeitet werden sollen, die gleiche UID (Eigentümer) haben wie das Skript selbst.


move_uploaded_file() ist von den normalen Safe Mode UID-Einschränkungen nicht betroffen. Dies ist nicht unsicher, da move_uploaded_file() nur mit via PHP hochgeladenen Dateien arbeitet.


Sollte die Zieldatei bereits existieren, wird sie überschrieben.

Siehe auch is_uploaded_file(), und das Kapitel Steuerung von Dateiuploads für ein einfaches Anwendungsbeispiel.

72 BenutzerBeiträge:
- Beiträge aktualisieren...
jsimon at redventures dot com
2.01.2011 6:41
Because of the way in which move_uploaded_file works, it can be difficult to debug without the proper error settings. If you are noticing that move_uploaded_file is not doing what you're expecting, and you are only getting a return value of false, try setting the error reporting at the top of your script.

ini_set("display_errors", 1);

This will ensure you are receiving any notices or warnings that you were missing before.
Ray.Paseur sometimes uses Gmail
18.08.2010 17:42
You can only move the uploaded file once.  You can use copy() if you need the file in more than one place.
<?php // RAY_temp_upload_example.php
"<pre>" . PHP_EOL;

if (!empty($_FILES))

if (!move_uploaded_file($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"])) echo "CANNOT MOVE {$_FILES["userfile"]["name"]}" . PHP_EOL;
    if (!
move_uploaded_file($_FILES["userfile"]["tmp_name"], $_FILES["userfile"]["name"])) echo "CANNOT MOVE {$_FILES["userfile"]["name"]}" . PHP_EOL;


<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="300000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
brentwientjes at NOSPAM dot comcast dot net
15.02.2010 20:34
I have for a couple of years been stymed to understand how to effectively load images (of more than 2MB) and then create thumbnails.  My note below on general file uploading was an early hint of some of the system default limitations and I have recently discovered the final limit  I offer this as an example of the various missing pieces of information to successfully load images of more than 2MB and then create thumbnails.  This particular example assumes a picture of a user is being uploaded and because of browser caching needs a unique number at the end to make the browser load a new picture for review at the time of upload.  The overall calling program I am using is a Flex based application which calls this php file to upload user thumbnails.

The secret sauce is:

1.  adjust server memory size, file upload size, and post size
2.  convert image to standard formate (in this case jpg) and scale

The server may be adjusted with the .htaccess file or inline code.  This example has an .htaccess file with file upload size and post size and then inline code for dynamic system memory.

htaccess file:
php_value post_max_size 16M
php_value upload_max_filesize 6M

//  $img_base = base directory structure for thumbnail images
//  $w_dst = maximum width of thumbnail
//  $h_dst = maximum height of thumbnail
//  $n_img = new thumbnail name
//  $o_img = old thumbnail name
function convertPic($img_base, $w_dst, $h_dst, $n_img, $o_img)
ini_set('memory_limit', '100M');   //  handle large images
unlink($img_base.$n_img);         //  remove old images if present
$new_img = $img_base.$n_img;
$file_src = $img_base."img.jpg"//  temporary safe image storage
move_uploaded_file($_FILES['Filedata']['tmp_name'], $file_src);
$w_src, $h_src, $type) = getimagesize($file_src);     // create new dimensions, keeping aspect ratio
$ratio = $w_src/$h_src;
   if (
$w_dst/$h_dst > $ratio) {$w_dst = floor($h_dst*$ratio);} else {$h_dst = floor($w_dst/$ratio);}

   switch (
1:   //   gif -> jpg
$img_src = imagecreatefromgif($file_src);
2:   //   jpeg -> jpg
$img_src = imagecreatefromjpeg($file_src);
3//   png -> jpg
$img_src = imagecreatefrompng($file_src);
$img_dst = imagecreatetruecolor($w_dst, $h_dst);  //  resample
imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $w_dst, $h_dst, $w_src, $h_src);
imagejpeg($img_dst, $new_img);    //  save new image

unlink($file_src);  //  clean up image storage

$p_id = (Integer) $_POST[uid];
$ver = (Integer) $_POST[ver];
$delver = (Integer) $_POST[delver];
convertPic("your/file/structure/", 150, 150, "u".$p_id."v".$ver.".jpg", "u".$p_id."v".$delver.".jpg");

wyverald at gmail dot com
12.12.2009 18:27
Another note concerning the following 2 warnings:

Warning: move_uploaded_file(/dest/inat/ion.ext) [function.move-uploaded-file]: failed to open stream: No such file or directory in /upload/handler.php on line X

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/temp/file/name' to '/dest/inat/ion.ext' in /upload/handler.php on line X

You would think that something is wrong about '/temp/file/name'... while actually, the real cause could be that the directory '/dest/inat' does not exist. Create this directory and the problem will likely get fixed.

Hope this helps.
gotrunko at hotmail dot com
13.11.2009 13:50
This function can upload many files whitout parameters in the declaration.
If one of them can not be uploaded, the function returns false and deletes all files that have been sent

function uploadFiles() {
$num_args = func_num_args();
$arg_list = func_get_args();
$valReturn = false;
$i = 0;
$unlinkElement = array();
$arg_list as $key=>$value) {
is_array($value) AND is_array($value[0])) {
$value[0]['error'] == 0 AND isset($value[1])) {
$value[0]['size'] > 0 AND $value[0]['size'] < 500000) {
$typeAccepted = array("image/jpeg", "image/gif", "image/png");
in_array($value[0]['type'],$typeAccepted)) {   
$destination = $value[1];
$value[2])) {
$extension = substr($value[0]['name'] , strrpos($value[0]['name'] , '.') +1);
$destination .= (str_replace(" ","-",$value[2])).".".$extension;
                        } else {
$destination .= $value[0]['name'];
move_uploaded_file($value[0]['tmp_name'],$destination)) {
$unlinkElement[] = $destination;
$i == $num_args) {
$valReturn = true;
    } else {
$unlinkElement as $value) {

To use this function you must specify an array with min two parameters like that.
NAME is an optional parameter !


= array($_FILES['file_one'],$destination,$name_one);
$file_two = array($_FILES['file_two'],$destination); //The name will be the same that $_FILES[]['name']

if(uploadFiles($file_one,$file_two)) {
} else {
chatchaw at tot dot co dot th
19.10.2009 9:03
When you use move_uploaded_file function to upload a file with utf-8  filename to linux system, you probably check your result by browsing to see the file in the target directory so please make sure that your terminal emulator or your samba configuration is set the character encoding to utf-8 otherwise  your file will be shown as ?????? (unreadable character).
brentwientjes at NOSPAM dot comast dot net
18.10.2009 9:21
I have looked at a lot of the file upload code listed below and other php documentation and have developed hopefully a robust single file upload routine.  I will later update with a multi file upload.  I have modestly tested the code.

//  5MB maximum file size
$MAXIMUM_FILESIZE = 5 * 1024 * 1024;
//  Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
$dir_base = "/your/file/location/";

$isFile = is_uploaded_file($_FILES['Filedata']['tmp_name']);
if (
$isFile)    //  do we have a file?
{//  sanatize file name
    //     - remove extra spaces/convert to _,
    //     - remove non 0-9a-Z._- characters,
    //     - remove leading/trailing spaces
    //  check if under 5MB,
    //  check file extension for legal file types
$safe_filename = preg_replace(
"/\s+/", "/[^-\.\w]+/"),
"_", ""),
    if (
$_FILES['Filedata']['size'] <= $MAXIMUM_FILESIZE &&
preg_match($rEFileTypes, strrchr($safe_filename, '.')))
$isMove = move_uploaded_file (

I use $isFile and $isMove later in the code for error recording.

Make sure your system has the appropriate file loading limits. This caused a lot of headeaches trying to figure out why some files loaded and some did not.   In my case I have an .htaccess file in the root of the web site with:

php_value post_max_size 16M
php_value upload_max_filesize 6M

You may also need to extend the execution time depending upon the amount of data being transferred.

(sorry if spacing of code is a little off.  it was hard to make the note editor like the code style.)
dt at NOSPAM dot windsor-county dot com
25.08.2009 16:10
Easiest way to find the extension of an uploaded file is to use:

= end(explode(".", $file['name']));
21.07.2009 20:33
$file_extension = pathinfo($path, PATHINFO_EXTENSION);
omgmain at gmail dot com
31.01.2009 5:23
This function should run perfectly, but just in case there are errors, this should help.
1 : Check that the syntax is correct. Remember, lines end with ;
2 : Make sure that the file you're moving exists, and that the place you're writing to is...well, writable. Sometimes, for instance with mac, they make the file so that it can only be accessed by the system, and you. You need to make it available for everyone.
3 : Make sure that the file size of the file you're uploading is under the max upload size. You can change the allowed file size on the php.ini file. The default value is 2M (M as in Megabytes). Also make sure that uploading is allowed
If you don't have any errors, then give yourself a pat on the back. BUT, to make your script more secure, put in your file:
str_replace('.', '', $_FILES['file name']['name']);
str_replace('/', '', $_FILES['file name']['name']);
Replace file name with the file name, and you're good to go!
nuabaranda at web dot de
14.01.2009 10:21
When using PHP on Windows and trying to use move_uploaded_file() on a file with special non-ascii characters like german umlauts they get converted bytewise thus destroying them resulting in a wrong CP1251 Windows filename.
Johan GENNESSON (php at genjo dot fr)
8.01.2009 15:08
sedativchunk post a method to find the extension of the file uploaded; but if the extension is more than 3 characters it won't work :

= '';
$file_type_length = strlen($file_type) - 3;
substr($file_type, $file_type_length);    //Will return PEG

Instead you can use :

echo strrchr('', '.');    //Will return .JPEG

Best regards
sedativchunk at gmail dot com
21.12.2008 23:13
I've been having alot of problems validating files on upload. The main problem I was having was matching up the type of file uploaded with the allowed file types, as when you use $_FILES['userfile']['type'], the variable contains the MIME type, which can change consistently from browser to browser.

A better method for simply finding the file type is to get the extension of the file by the file name. Here is an example of getting a file type and matching it to an allowed file type (then uploading it and doing some other validation):
// Declare variables
    // Get the basic file information
$userfile = $_FILES['userfile']['name'];
$file_size = $_FILES['userfile']['size'];
$file_temp = $_FILES['userfile']['tmp_name'];
$file_err = $_FILES['userfile']['error'];
$path = 'images/';

// Create a new file name
    // This is so if other files on the server have the same name, it will be renamed
$randomizer = rand(0000, 9999);
$file_name = $randomizer.$userfile;

// Get the file type
    // Using $_FILES to get the file type is sometimes inaccurate, so we are going
    // to get the extension ourselves from the name of the file
    // This also eliminates having to worry about the MIME type
$file_type = $userfile;
$file_type_length = strlen($file_type) - 3;
$file_type = substr($file_type, $file_type_length);

$userfile)) {
'<div style="font-weight: bold; padding: 6px;">File Uploaded  Information</div>
        <li>Original File Name: '
.$userfile. '</li>
        <li>New File Name: '
.$file_name. '</li>
        <li>File Type: '
        <li>File Size: '
.$file_size. '</li>
        <li>File Temporary Name: '
.$file_temp. '</li>
        <li>Fille Error: '
. $file_err. '</li>

// limit the size of the file to 200KB
if($file_size > 25600) {

// Set allowed file types
        // Set case of all letters to lower case
$file_type = strtolower($file_type);
$files = array();
$files[] = 'jpeg';
$files[] = 'jpg';
$files[] = 'gif';
$files[] = 'png';
// Search the array for the allowed file type
$key = array_search($file_type, $files);
$key) {
'<b>File allowed!</b><br />';
        } else {
'<b>ILLEGAL FILE TYPE</b><br />';

// Check for errors and upload the file
$error_count = count($file_error);
$error_count > 0) {
$i = 0; $i <= $error_count; ++$i) {
} else {
move_uploaded_file($file_temp, 'images/' .$file_name.'')) {
'<h3>Upload Successful!</h3>';
        } else {

    } else {
'<h3>No file has been selected.</h3>';

In this script, I am looking for image types JPG, PNG, and GIF. You could easily add other types of files ex.
$files[] = "docx";
$files[] = "txt";

I am running into some trouble getting PNG's to upload properly, but this is the best method for validating the file type I believe.
24.11.2008 21:44
I had exactly than" tnp at shaman dot co dot uk" : move_uploaded_file() is either asynchronous or uses some kind of virtual filesystem. But you may encounter big problems when trying to access a file just uploaded, especially is you tried to change the name.
If you have problems where the uploaded file seems unaccessible, try to use copy() instead.
Dan Delaney
13.10.2008 22:41
For those using PHP on Windows and IIS, you SHOULD set the "upload_tmp_dir" value in php.ini to some directory around where your websites directory is, create that directory, and then set the same permissions on it that you have set for your websites directory. Otherwise, when you upload a file and it goes into C:\WINDOWS\Temp, then you move it to your website directory, its permissions will NOT be set correctly. This will cause you problems if you then want to manipulate that file with something like ImageMagick's convert utility.
all.marx {at} gmail
26.09.2008 2:34
Here's a function I wrote to take care of business.

# Written by Sean Nall <all.marx {at} gmail>
# and placed into the public domain.

# @function upload_file
# @param $field        string        the name of the file upload form field
# @param $dirPath    string        the relative path to which to store the file (no trailing slash)
# @param $maxSize    int            the maximum size of the file
# @param $allowed    array        an array containing all the "allowed" file mime-types
# @return mixed        the files' stored path on success, false on failure.
function upload_file($field = '', $dirPath = '', $maxSize = 100000, $allowed = array())
    foreach (
$_FILES[$field] as $key => $val)
$key = $val;

    if ((!
is_uploaded_file($tmp_name)) || ($error != 0) || ($size == 0) || ($size > $maxSize))
false;    // file failed basic validation checks

if ((is_array($allowed)) && (!empty($allowed)))
        if (!
in_array($type, $allowed)) 
false;    // file is not an allowed type

do $path = $dirPath . DIRECTORY_SEPARATOR . rand(1, 9999) . strtolower(basename($name));
    while (

    if (
move_uploaded_file($tmp_name, $path))



if (array_key_exists('submit', $_POST))  // form has been submitted
    if (
$filepath = upload_file('music_upload', 'music_files', 700000, array('audio/mpeg','audio/wav')))
'File uploaded to ' . $filepath;
'An error occurred uploading the file... please try again.';
        <form method="post" action="'
.$_SERVER['PHP_SELF']. '" enctype="multipart/form-data">
            <input type="file" name="music_upload" id="music_upload" />
            <input type="submit" name="submit" value="submit" />

print_r($_FILES);     // for debug purposes

Mark Kazemier
27.08.2008 18:36
I have the same problem as the person two comments below me. When I use the move_uploaded_file function the permissions for the file are set to 0600. No matter what configurations you set.

I searched the internet and I found more people with the same problems, but no solutions. I set the umask of apache to 013 and still the files were set to 0600.

The copy function solves the problem. Another way to solve this problem is using the chmod function after uploading.
agan0052 at hotmail dot com
23.08.2008 21:22

$target = "galleries/".basename($_FILES['uploaded']['name']) ;

move_uploaded_file($_FILES['uploaded']['tmp_name'],$target)) echo "OK!";//$chmod o+rw galleries

"<form enctype='multipart/form-data' action='CodeTool.php' method='POST'>";
"File:<input name='uploaded' type='file'/><input type='submit' value='Upload'/>";

Florian S. in H. an der E. [.de]
17.08.2008 23:02
move_uploaded_file (on my setup) always makes files 0600 ("rw- --- ---") and owned by the user running the webserver (owner AND group).
Even though the directory has a sticky bit set to the group permissions!
I couldn't find any settings to change this via php.ini or even using "umask()".

I want my regular user on the server to be able to "tar cjf" the directory .. which would fail on files totally owned by the webserver-process-user;
the "copy(from, to)" function obeys the sticky-bit though!
milan dot matejcek at seznam dot cz
5.08.2008 1:59
     * function for upload file
     * @version 5.8.2008
     * @author Milan Matejcek
     * @param $GLOBALS        $files        -$_FILES[], array with keys error, size, type etc.
     * @param string        $dir_out    -dir, where save file without filename
     * @param string        $up_file    -regular string for MIME type
     * @param int            $max_size    -maximum size [B]
     * @param bool|string    $f_name        -true    =new automatic filename
     *                     -false    =original filename
     *                     -string    =forexample picture.jpg, you will write only 'picture'
     * @return string        -filename
function upload_file($files, $dir_out, $up_file='/', $max_size=null, $f_name=true){

$files['error'] == 0){
preg_match("~$up_file~", $files['type'])){

$max_size === null){
$max_size    =$files['size'];

$files['size'] > 0 && $files['size'] <= $max_size){
$name        =array_reverse(explode('.', $files['name']));
$koncovka    ='.'.strtolower($name[0]);
$f_name === true){
$files['name']    =substr(md5(microtime()),0,6).$koncovka;
                    }elseif (
$files['name']    =$f_name.$koncovka;
                      * }else{
                      * $files['name'] =$files['name'];
                      * }

$filename    =$files['name'];

$m    =@move_uploaded_file($files['tmp_name'], $dir_out.$files['name']);
$m === false){
//soubor se nepodarilo nahrat
return array(
'num'    =>    4,
'text'    =>    'selhala move_uploaded_file'
//file is big
return array(
'num'    =>    5,
'text'    =>    'soubor je moc velky'

//false MIME type
return array(
'num'    =>    6,
'text'    =>    'neodpovidajici typ souboru, kontrola na '.$up_file.' vysledek '.$files['type']
//$_File['error'] return 1
return array(
'num'    =>    7,
'text'    =>    'selhalo nahrani souboru na server'


jphansen at uga dot edu
26.03.2008 16:41
I was getting error code 1 (, indicating that the file exceeded upload_max_filesize. To fix this, run <?php ini_set("upload_max_filesize", "100M"); ?> (then restart apache) changing the second argument to your limit.
Jelmer Jellema
20.02.2008 23:59
In response to tnp at shaman dot co dot uk

This is probably not a bug, nore proof of any meta filesystem running. My guess would be the uploaded file would have its file access set to 0600 (u=rw,go=), so it could not be read by the mysql server (running under a differt uid as your script).
Copy probably applies the normal umask to the file, with as a result permissions something like 0644: readable for anyone and the mysql server.
rasponline at gmail dot com
14.01.2008 16:43
Basic difference between Copy() and move_uploaded_file() function i have noted is that, Copy function upload the file from any source to any destination on the server!!!!
While move_uploaded_file() can moved the uploaded file from temp server location to any destination on the server!!!
iim dot vxk at gmail dot com
7.09.2007 11:38
when you get this 2 Warnings - paths are a real sample - ::

move_uploaded_file(/uploads/images/sample.png) [function.move-uploaded-file]: failed to open stream: No such file or directory in /scripts/php/system/upload-file.php on line X


move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/somefilename' to '/uploads/images/sample.png' in /scripts/php/system/upload-file.php on line X

probably the path '/uploads/images/sample.png' is incomplete, in my case the complet path is "/home/x-user/public_html/uploads/images/sample.png"

you can use getcwd() to know the current working directory.

tnp at shaman dot co dot uk
4.09.2007 22:20
I may well also report this as a bug, but in the meantime I'll document what I have seen so far.

What I wanted to do, was take the uploaded temporary filename and pass it to MySql as part of a load_file  injection into a BLOB to store the file contents.
My first attempt was of an indexed array..

$filesize= $_FILES[$index]["size"];

followed by:-

$query=sprintf("insert into project_files set project_id='%s',current='yes',date='%s' ,user='%d', size='%d', description='%s', name='%s', content=LOAD_FILE('%s')",

This gave me an empty content field. Zero bytes.

I took the echo $query string and applied it to MtySql interpreter. With a suitable filename it worked..well after fixing a few database perms..but no joy from PHP still.

I found this page and tried adding:-


before setting up the query.

That was worse. The browser just hung on me.

Then I tried

That worked!?*&^

My CONJECTURE is that PHP has a meta file-system running, and the environment the program sees is NOT reflected into the OS (Debian etch, in this case) until it closes and flushes its buffers.

Copy() does in fact seem to result in a real disk write, but move_uploaded_file() does not, so when the name was passed to Mysqld, it didn't exist, or had zero content or maybe with a large file truncated content.

I can see the point of this: faster response for web browsers who get the program HTTP output over with before it settles down to its housekeeping, but in this case it's a bit of a disaster.

What is needed is a way to force the buffers to flush if that is what is going on...
bdeguire at yahoo dot com
20.07.2007 15:16
For Windows users: Note that your file attributes from the temporary upload folder ("upload_tmp_dir" in php.ini) are copied to the destination folder (because the file is *moved*, not copied).

I ran into a problem when using Microsoft Indexing Server. For it to work, you must activate the attribute "For fast searching,  allow Indexing Server to index this folder". However, you must not only activate this attribute on the *final* destination folder, but ALSO on the temporary upload folder. So when the file is move from the temp location to the final location, the attributes are kept.
17.07.2007 18:50
In response to bogusred,

The user and group PHP runs as is not always 'nobody', but is environment dependant. Typically Apache runs as root and changes into another user as specified in the httpd.conf.  'nobody' is a common default setting, but 'apache' or another user is equally likely and in a shared hosting environment you often find it runs as one of various users depending on the virtual host.
nlgordon at iastate dot edu
16.07.2007 20:29
Just a helpful comment.  If you have open_basedir set then you must set upload_tmp_dir to somewhere within the open_basedir.  Otherwise the file upload will be denied.  move_uploaded_file might be open_basedir aware, but the rest of the upload process isn't.
10.06.2007 22:58
If you have a directory in a *nix environment where you store all of your file uploads and your php script only seems to work when permissions for that directory are set to 777, here's how to fix it so that you can have the security benefits of 755 while still allowing your php scripts to work, including the move_uploaded_file().

through shell access, navigate to the directory that contains your uploads folder and run the following 2 commands:
chown -R nobody uploaddir
chmod -R 755 uploaddir

Replace 'uploaddir' with the name of your uploads directory. The first command changes the owner of the directory and files to 'nobody' which is what php operates under. The second changes the folder and files to only allow user access to writing. This is much more secure.

Hopefully this will help someone out there who had the same problem as me.
masahigo at gmail dot com
30.05.2007 17:49
I found a great resource concerning uploads with PHP:

They explain and tell how to optimize PHP installation to handle large file uploads. Helped me a lot!
info at arpadh dot hu
4.05.2007 20:02
Values upload_max_filesize and post_max_size (ie. php.ini values) cannot be modified in runtime with ini_set() function.
If you are using Apache web server, use .htaccess files with an IfModule replacing values corresponding to your file size and PHP version:

<IfModule mod_php4.c>
php_value upload_max_filesize 50M
php_value post_max_size 50M

- means 50MB upload limit.
booc0mtaco at gmail dot com
19.04.2007 17:45
Also, make sure that the setting for the post_max_size allows for a proper file size range.

post_max_size = 128M ; Expands the size of POST data for file uploads
guy at neonxero dot net
22.03.2007 16:47
I could be wrong, but I usualy use

$uploadext = strtolower(strrchr($imagename,"."));

to find the file extension when uploading, as opposed to explode().

13.03.2007 11:55
To allow bigger upload file size, edit in php.ini and change:

upload_max_filesize =350M ; Maximum allowed size for uploaded files.
post_max_size =360M ; Maximum size of POST data that PHP will accept.

max_execution_time =5000 ; Maximum execution time of each script, in seconds

In adition, you must change the server configuration as well.
For apache server, edit httpd.conf.
7.03.2007 6:38
the dot only dot storm at gmail dot com wrote:
> In addition to the file extension checking. A simply way
> of getting the extension (regardless of size):
> $efilename = explode('.', $filename);
> $ext = $efilename[count($efilename) - 1];

How about:

$ext = end(explode('.',$filename));
dev at rising9 dot com
6.03.2007 14:07
Can you explain more, how i can change the max memory size varible for the large file can work with move_uploaded_file()?
Thanks a lot!

3.02.2007 16:32
If you find that large files do not upload in PHP even though you've changed the max_upload_size , this is because you need to change the max memory size varible too. The entire file is loaded into memory before it is saved to disk.
1.11.2006 11:49
If you want to change the filename used by the user then use this code (static name). This example uploads a pdf magazine to a website and always overwrites the file that is there. Use if you are dealing with a small number of files or people who know what they're doing!

$uploaddir = $_SERVER['DOCUMENT_ROOT']."/dainsider/magazines/";
//Copy the file to some permanent location
if(move_uploaded_file($_FILES["magfile"]["tmp_name"], $uploaddir))
"Magazine Updated!";
"There was a problem when uploding the new file, please contact ".$admin_email." about this.";
shacker at birdhouse dot org
10.09.2006 9:28
If you're dealing with files uploaded through some external FTP source and need to move them to a final destination, searching for "mv" or "move" won't get you what you want. You want the rename() function.

(move_uploaded_file() won't work, since the POST vars won't be present.)
Rob Szarka
23.08.2006 3:18
Apparently the warning above might better be written "If the destination file already exists, it will be overwritten ... regardless of the destination file's permissions."

In other words, move_uploaded_file() executes as if it's root, not the user under which the web server is operating or the owner of the script that's executing.
sergeygrinev at mail dot ru
20.04.2006 23:04
small typo:

$fulldest = $dest.$newfilename;

show be

$fulldest = $dest.$filename;

or you would have infinite loop.
14.04.2006 3:12
nouncad at mayetlite dot com posted a function that uploaded a file, and would rename it if it already existed, to filename[n].ext

It only worked for files with extensions exactly three letters long, so I fixed that (and made a few other improvements while I was at it).

// Usage: uploadfile($_FILE['file']['name'],'temp/',$_FILE['file']['tmp_name'])
function uploadfile($origin, $dest, $tmp_name)
$origin = strtolower(basename($origin));
$fulldest = $dest.$origin;
$filename = $origin;
  for (
$i=1; file_exists($fulldest); $i++)
$fileext = (strpos($origin,'.')===false?'':'.'.substr(strrchr($origin, "."), 1));
$filename = substr($origin, 0, strlen($origin)-strlen($fileext)).'['.$i.']'.$fileext;
$fulldest = $dest.$newfilename;
  if (
move_uploaded_file($tmp_name, $fulldest))
jessy dot diamondman at gmail dot com
5.04.2006 18:29
I am pretty new, and am having upload problems myself, but I think I can help out ffproberen2 at dodgeit dot com with his premission denied errors. I had these two, and I had to change the upload directory, not the tmp_upload_dir or what ever it is called. The move_uploaded_file meathod takes an upload location as the last parameter. I am running a bundled package of Apache, Php, mySQL and so on, and on mine, specifing a directory of '' will upload it into C:\Program Files\xampp\apache (my PC is my experimental server, I will get linux, but got to obtain it and internet cuts off after 196mb so can't download it) even though php file is in C:\Program Files\xampp\htdocs\xampp\jessyexum\upload_client.php.

This is a code that I found and then modified, hope it can help. It dosn't always upload every file type giving me an error #2.

= '';
$uploadfile = $uploaddir . basename($_FILES['upfile']['name']);
if (
move_uploaded_file($_FILES['upfile']['tmp_name'], $uploadfile)) {
"File is valid, and was successfully uploaded.\n";
} else {
"Possible file upload attack!\n";
'Here is some more debugging info:';
ffproberen2 at dodgeit dot com
5.04.2006 12:02
On windows I made the directory writable, by changing the Apache httpd.conf file.

The problem I had, was with the upload directory. The move_uploaded_file produced an error like: failed to open stream: Permission denied.

I changed my php.ini to specify an upload directory:
  upload_tmp_dir = "d:/temp/php/uploads/"

and I added the following in the Apache hpptd.conf file:

  <Directory "D:/temp/php/uploads">
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all

restarted Apache, and the upload succeeded.
j dot m dot thomas at NOSPAM dot blueyonder dot co dot uk
16.02.2006 9:26
To retrieve the file extension, and various other information about the path, it is easiest to use the pathinfo function.

= pathinfo('/www/htdocs/index.html');

$path_parts['dirname'], "\n";
$path_parts['basename'], "\n";
$path_parts['extension'], "\n";

Would produce:

adam at darkhousemedia dot com
3.02.2006 7:35
To retrieve the file extension, I think this example makes more sense than the one below. 

$ext = explode(".", $file);
$ext = array_pop($ext);

It doesn't have to count() the array and then subtract 1 to point to the proper array element, it simply isolates the last element of the array, and discards everything else.
calamitoso at gmail dot com
31.01.2006 20:10
to separate (for example) images from other file types among the uploaded files you can check the MIME type also (thus making the file extension check unnecessary)

$temp = strpos($_FILES["pic"]["type"], "image");
if ($rep===FALSE){
   //the strpos function will return a boolean "false" ONLY if the needle string is not found within the haystack
   echo "is not an image";
   echo "is an image";
mancow at macfilez dot net
10.01.2006 6:31
To nouncad at mayetlite dot com,

That function will work fine for files with a 3-character file extension.  However, it is worth noting that there are valid, registered file extensions that are longer than 3 characters.  For example, a JPEG file can be denoted by *.jpg (and others), but it can also have *.jpeg as a valid extension.  Check out for a good reference of file extensions.

The best bet to me would be parsing the uploaded file's name ($_FILES['uploadedfile']['name']) based on the presence of dots.  Another wrench in the gears:  a file can have dots in the filename.  That's easy enough to handle -- just explode() the file name and hope that the last element in the array it gives you is the file extension (you can always validate it if you're so inclined).  Then just piece it together in a string accordingly by stepping through the array (don't forget to add those dots back to where they were!), appending a guaranteed unique string of characters (or enumerate it like you were doing, keeping track via a loop), and finally tacking on the file extension.

You may have other mechanisms for verifying a file's extension, such as a preg_match on the whole name, using something like "/\\.(gif|jpg|jpeg|png|bmp)$/i" (more can, of course, be added if you so desire) for the most common types of images found on the web.

For blindly guaranteeing an uploaded file will be uniquely named, this seems like a fantastic way to go.  Enjoy!
AT-HE (at_he AT hotm4il DOT com)
16.12.2005 7:28
Note that post_max_size also needs to be considered, by default it is 8M. I raised my upload_max_filesize to 20M and was wondering why 10M uploads weren't working...

r: It could be because of your max execution time.

try changing the value of both post_max_size and upload_max_filesize
nouncad at mayetlite dot com
14.12.2005 14:30
Great!! my first note here...

This function upload a file.
If file exist, create a copy as "filename[n].ext"

function subirFichero($origen, $destinoDir, $ftemporal) {   
$origen = strtolower(basename($origen));

$destinoFull = $destinoDir.$origen;
$frand = $origen;
$i = 1;
    while (
file_exists( $destinoFull )) {
$file_name         = substr($origen, 0, strlen($origen)-4);
$file_extension  = substr($origen, strlen($origen)-4, strlen($origen));
$frand = $file_name."[$i]".$file_extension;
$destinoFull = $destinoDir.$frand;
    if (
move_uploaded_file($ftemporal, $destinoFull))    return $frand;
    else                                                 return
7.11.2005 22:27
Microsoft returns image/pjpeg not image/jpg when using $_FILES['imageName']['type'];
7.11.2005 21:44
move_uploaded_file()'s return codes are not allways obious !

Unable to move '/var/tmp/phpuuAVJv' to '/home/me/'

will apear if your disk is full, or the webserver (www user) exeeded it's disk qouta. (probably some others)

i dont know if its a bug (just not iplemented) or a feature (to hide from 3rd parties details about the system or about the specific error) ?

it happend to me that after several months of successful operation, the disk filled up and qouta exeeded.

it took me long time, finding out why all the sudden my scripts didnt work properly anymore.

5.11.2005 0:56
Note that post_max_size also needs to be considered, by default it is 8M. I raised my upload_max_filesize to 20M and was wondering why 10M uploads weren't working...

It could be because of your max execution time.

29.10.2005 5:44
Note that post_max_size also needs to be considered, by default it is 8M. I raised my upload_max_filesize to 20M and was wondering why 10M uploads weren't working...
jest3r at mtonic dot net
20.10.2005 6:10
It seems that move_uploaded_file use the GROUP permissions of the parent directory of the tmp file location, whereas a simple "copy" uses the group of the apache process. This could create a security nighmare if your tmp file location is owned by root:wheel
6.09.2005 17:46
If the user try to upload a too bigger file then the upload procedure will fail even if u have established an error message.
How to avoid this problem? there's my solution:

(max_file_size = 2,50 MB)

$fsize = $_FILES["userfile"]["size"];

if($fsize == 0 || $fsize > 2621000) exit("keep the filesize under 2,50MB!!");

When the size is bigger than the MAX_FILE_SIZE field, the value of $fsize is equal to 0 (zero) ......
espiao at gmail dot com
27.07.2005 16:25
 * This function moves the archives and directoryes of a directory of
 * origin for a directory destination being able replace them or not.

function mvdir($oldDir, $newDir, $replaceFiles = true) {

    if ($oldDir == $newDir) {
        trigger_error("Destination directory is equal of origin.");
        return false;
    if (!($tmpDir = opendir($oldDir))) {
        trigger_error("It was not possible to open origin directory.");
        return false;

    if (!is_dir($newDir)) {
        trigger_error("It was not possible to open destination directory.");
        return false;       

    while (($file = readdir($tmpDir)) !== false) {

        if (($file != ".") && ($file !== "..")) {
            $oldFileWithDir = $oldDir . $file;
            $newFileWithDir = $newDir . $file;
            if (is_dir($oldFileWithDir)) {
                @mkdir($newFileWithDir."/", 0777);
                @mvdir($oldFileWithDir."/", $newFileWithDir."/", $replaceFiles);

            else {
                if (file_exists($newFileWithDir)) {
                    if (!$replaceFiles) {
                @copy($oldFileWithDir, $newFileWithDir);
                @chmod($newFileWithDir, 0777);
    return true;

 * This is an example of move with replace files on destination folder if
 * exists files with the same names on destionatio folder
mvdir("/var/www/example/", "/var/www/other_folder/");

 * This is an example of move without replace files on destination
 * folder if  exists files with the same names on destionatio folder
mvdir("/var/www/example/", "/var/www/other_folder/", false);
18.05.2005 23:51
move_uploaded_file apparently uses the root of the Apache installation (e.g. "Apache Group\Apache2" under Windows) as the upload location if relative pathnames are used.

For example,
$ftmp = $_FILES['userfile']['tmp_name'];
$fname = $_FILES['userfile']['name'];
move_uploaded_file($ftmp, $fname);
moves the file to
"Apache Group\Apache2\$fname";

In contrast, other file/directory related functions use the current directory of the php script as the offset for relative pathnames.  So, for example, if the command


is called from 'Apache Group\Apache2\htdocs\testpages\upload.php', the result is to create
'Apache Group\Apache2\htdocs\testpages\tmp'

On the other hand, if 'mkdir' is called just before 'move_uploaded_file', the behavior changes.  The commands,

move_uploaded_file($ftmp, $fname);

used together result in

"Apache Group\Apache2\htdocs\testpages\tmp\$fname"

being created.  Wonder if this is a bug or a feature.

4.04.2005 23:29
Instead of using chdir or chmod 0777 a safer alternative to move_uploaded_files is to use PHP's ftp functions to move the file into a web dir.

1. Make ftp connection to with the correct username and password.
2. ftp_chdir to the required directory.
3. ftp_put ($_FILES['myfile']['tmp_name'], $finalfilename);
4. ftp quit.
richardNO at SPAMbesite dot nl
11.03.2005 13:32
Creating the dir with mkdir from php is a security risk too. Everyone who can run a php script on the server can write a script to mess with the dir.
user at php dot net
1.03.2005 11:54
Giving the directory 777 permission is not a good idea for security reasons, it would be better to create the directory using "mkdir()".

That will make php user (usually "nobody") the owner of the directory, and permissions will not be a problem.
Michel S
16.02.2005 18:41
I once had a problem with this function. File was uploaded correctly, but I still had to chmod the file afterwards. It could not be used otherwise.

Michel S
allan666 at NOSPAM dot gmail dot com
17.12.2004 7:35
On the Fedora Core 3 Linux distribution, you may get a "failed to open stream: Permission denied in ..." message. I fact changing the permission of the directory will not work (even if you set to 0777). It is because of the new SELinux kernel that allow apache user to write only in /tmp dir (I think). In order to solve the problem you must to disable the SELinux (at least for apache service) to allow the server to write in other directories. To do that, run the system-config-securitylevel app and disable the SE to apache service. Reboot your system and continue your work. Hope it helps!
php at f00n dot com
4.07.2004 10:17
If you are building an intranet framework and use NAT/Routing heed the following advice.

If you want to move uploaded files to an FTP server you cannot use the ftp wrapper (ie. 'ftp://user:pass@ftpserver/') as part of your move_uploaded_file() action.  This is due to the wrapper only using passive mode with ftp.

The only workaround is using the ftp functions (may not be compiled by default with *nix but is by default with windows).
froid_nordik at sympatico dot ca
4.06.2004 18:26
Make sure the directory you are moving the file to exists before using this command.
sauron at nospam on morannon dot org
8.03.2004 14:20
An extension only does not really tell you what type of file it really is. I can easily rename a .jpg file to a .zip file and make the server think it is a ZIP file with webmaster kobrasrealm's code.

A better way is to use the Linux utility "file" to determine the file type. Although I'm aware that some users might use Windows on their webservers, I thought it's worth  mentioning the utility here. Using the backtick operators and preg_matches on the output, you can easily determine the file type safely, and fix the extension when necessary.
mail at johan dot it
28.02.2004 23:14
Warning: If you save a md5_file hash in a database to keep record of uploaded files, which is usefull to prevent users from uploading the same file twice, be aware that after using move_uploaded_file the md5_file hash changes! And you are unable to find the corresponding hash and delete it in the database, when a file is deleted.
mina86 at tlen dot pl
8.12.2003 0:03
Hey! Why not using strrchr() to get file  extension:
<?php $ext = strrchr($_FILES['file']['name'], '.'); ?>
or to get it without '.' at the begining:
<?php $ext = substr(strrchr($_FILES['file']['name'], '.'), 1); ?>

If you want to update file without any strang characters you can use:
$dir . preg_replace('/[^a-z0-9_\-\.]/i', '_', $_FILES["file"]["name"])
wolke74 at web dot de
18.11.2003 18:02
French and English filenames --- as it is not forbidden -- often have an apostrophy, for instance "That's advertisement paper.doc" or "Les aventures d'Alice dans le pays du miracle.doc". However, uploading such files can run into trouble.

So you can write, if the posted file had been marked by myfile .

     echo "Something is wrong with the file";

9.11.2003 4:54
The example to find file extension bellow is quite confusing and its using to much code for a much simpler solution. Which is in example:

$file_parts = pathinfo('dir/' . $_FILES['file']['name']);
$file_extension = strtolower($file_parts['extension']);

The 'dir/' part is only to get a valid path.
www at w8c dot com
9.10.2003 9:03
function upload($filedir,$source,$source_name,$up_flag,$lastname)
    if (!file_exists($filedir))
    if (!$lastname)
    if (file_exists("$filedir/$lastname"))
        if ($up_flag=="y")
            echo "$source_name OK<br>";
        echo "$source_name ...<br>";
        echo "$source_name OK<br>";
allen at brooker dot gb dot net
12.02.2003 11:48
The first comment totally threw me off. Under the 'new regime', the 'string filename' is $_FILES['userfile']['tmp_name']

Also note that the 'string destination' should be the full path and filename. As long as your server isnt using virtual hosting, you should be able to use $_SERVER['DOCUMENT_ROOT'] . "path/within/website". This'll save hours of hassle trying to get sometimes ignorant ISPs to give you your full and 'no symlinks' path.


PHP Powered Diese Seite bei
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)