PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 26th, 2013, 06:11 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
Question A question about the move_uploaded_file function

Dear all,

Quote:
OS: Fedora Core 17 (x86_64)
Browser: Firefox 19.0
PHP version: 5.4.11
Apache version: 2.2.22


I have a question about the move_uploaded_file function. Currently I'm reading the Chapter 9: Handling HTML Forms with PHP of a very interesting book named Beginning PHP 5.3 in order to learn how to upload files using forms.

Here is the test scenario. We have a very simple form including just a file browser allowing to select an image file followed by a submit button for sending the image to the server (I'm working on my laptop, both client and the server are actually on the same physical machine).

What I want to do is to let the user select an image from the local hard drive, submit it to the server and once received at the PHP server side, the image will be shown in the browser within an <img> element (it's just a first example for learning how to upload a file)

The following code that I've written is not 100% complete and it lacks some additional tests such as verifying the uploaded file type, the length of the file name, etc. But it is normally (I think) enough at least for a file upload. So here is what I have written so far
Code:
<!doctype html>
    <head>
        <title>MyPHPScript</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <?php
            /* If the user has already pushed
             * the submit button, just show
             * the submitted image in the browser
             * by calling the printImage() function
             */
            if (array_key_exists("submit", $_POST))
                printImage();
            else
                homePage();
        ?>
        
        <?php
            function homePage()
            {
        ?>
                <!--
                    * So here is the form that I use to
                    * upload an image. It's very simple
                    * just a file browser and a submit
                    * button to send the image
                -->
                <form action="myscript.php" method="post" 
                    enctype="multipart/form-data">
                    <label for="fileBrowser">Image file</label>
                    <input type="file" name="fileBrowser" id="fileBrowser"
                        value=""/>
                    <input type="submit" name="submit" id="submit" 
                        value="submit"/>
                </form>
        <?php
            }
        ?>
        
        
        <?php
            function printImage()
            {
                /*
                 * So here I check whether the file
                 * was uploaded successfully. If it
                 * is the case, then I move the file
                 * from the PHP tmp directory to a 
                 * directory named "images" in the 
                 * DOCUMENT_ROOT. And finally I will
                 * show the image by <img> within
                 * the browser
                 */
                if (array_key_exists("fileBrowser", $_FILES))
                {
                    switch ($_FILES["fileBrowser"]["error"])
                    {
                        case UPLOAD_ERR_OK:
                        {
                            $fileInfo = $_FILES["fileBrowser"];
                            $fileName = $fileInfo["name"];
                            $filePath = $fileInfo["tmp_name"];
                            $fileAbsPath = $filePath . "/" . $fileName;
                            $destPath =  $_SERVER["DOCUMENT_ROOT"] . 
                                "/images/" . $fileName;
                            if (move_uploaded_file($fileAbsPath, $destPath)) 
                                echo "Ok the file was successfully moved";
                            else
                                echo "No the file was not moved";
                            break;
                        }
                        case UPLOAD_ERR_INI_SIZE:
                        case UPLOAD_ERR_FORM_SIZE:
                        {
                            echo "The file is too big";
                            break;
                        }
                        case UPLOAD_ERR_NO_FILE:
                        {
                            echo "no file was uploaded";
                            break;
                        }
                        case UPLOAD_ERR_NO_TMP_DIR:
                        {
                            echo "Permission denied: no access to tmp dir";
                            break;
                        }
                        case UPLOAD_ERR_CANT_WRITE:
                        {
                            echo "File cannot be written on server HDD";
                            break;
                        }
                        case UPLOAD_ERR_EXTENSION:
                        {
                            echo "File upload stopeed by one of ".
                                "PHP extensions";
                            break;
                        }
                        default:
                            echo "Please contact the administrator";
                    }
                }
                else
                    echo "no file was uploaded";
        ?>
                <img src="<?php echo "$destPath"; ?>">
        <?php
            }
        ?>
          
    </body>
</html>
According to what I checked in the switch statement, case UPLOAD_ERR_OK is executed when I upload the file but move_uploaded_file() returns False and I don't understand why. Under Linux I gave all privileges on the images/ directory (chmod -R 777 images/) so I don't think there is a privilege problem.

Also I checked the following directives from the output of php info:

- file_uploads : On
- max_file_uploads : 20
- upload_max_filesize: 2M
- upload_tmp_dir: No value


upload_tmp_dir has no value, but then I checked the online documentation about this directive and here is what I found:
http://www.php.net/manual/en/ini.core.php#ini.upload-tmp-dir
Quote:
... The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default. ...
Consequently, albeit not specified, PHP takes the default system tmp dire, which in the case of linux as I understand is /tmp.

There is also something that I found strange, as you can see I query the following information in my code
Code:
. . . 
$fileInfo = $_FILES["fileBrowser"];
$fileName = $fileInfo["name"];
$filePath = $fileInfo["tmp_name"];
$fileAbsPath = $filePath . "/" . $fileName;
$destPath =  $_SERVER["DOCUMENT_ROOT"]
. . . 
Just for some debugging I did some echo within my code on these variables just to see whether they return something. However there is something strange:

- $fileInfo["tmp_name"] returns the path to the directory inluding the file and not the absolute path to the file (that is, the absolute path of the file without the filename)

- Once the PHP script is run, if I open a linux terminal and try to see the content of the directory returned by $fileInfo["tmp_name"], there is nothing there.

I'm really confused.

Could you kindly make some clarification?


Regards,
Dariyoosh

Reply With Quote
  #2  
Old February 26th, 2013, 06:51 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,944 E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)E-Oreo User rank is General 91st Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 10 h 16 m 54 sec
Reputation Power: 7053
$fileInfo['tmp_name'] contains the absolute path to the file, appending $fileInfo['name'] to it isn't correct. move_uploaded_file should receive $fileInfo['tmp_name'] directly as its first argument.
__________________
PHP FAQ
How to program a basic, secure login system using PHP
Connect with me on LinkedIn


Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #3  
Old February 27th, 2013, 02:35 AM
dariyoosh's Avatar
dariyoosh dariyoosh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Location: Iran / France
Posts: 132 dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level)dariyoosh User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 6 h 25 m 17 sec
Reputation Power: 133
Well, this is one of the strange things that happen. Because as you said and also according to the book, _FILES[". . ."]["tmp_name"] return the absolute path to the file (image file in the case of my program). Yet when I write the following in my program:
Code:
$fileInfo = $_FILES["fileBrowser"];
echo $fileInfo["tmp_name"];
I got a strange directory name inside the /tmp (each time that I run the script, this strange name changes)
Code:
/tmp/phpjWlf32
So the very fact that it doesn't give me the absolute path (unlike what has been spcecified in PHP specification) is not normal, could this be related to php.ini?

Thanks,

Regards,
Dariyoosh

Reply With Quote
  #4  
Old February 27th, 2013, 03:25 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 21 m 47 sec
Reputation Power: 813
Hi,

Quote:
Originally Posted by dariyoosh
So the very fact that it doesn't give me the absolute path (unlike what has been spcecified in PHP specification) is not normal, could this be related to php.ini?


This is an absolute path. It's a file called "phpjWlf32" in the "tmp" directory below the root directory. What else did you expect? A Windows-style path like "C:\php\tmp\upload.jpg"?

The file name is chosen randomly, because taking the user-defined name would obviously a bad idea -- you'd get collisions all the time. PHP also doesn't append the user-defined file extension

You should actually be very careful with where you put those files. As far as I can I see, you just move them into the document root. That's a bad idea, because people can then upload their own PHP scripts and execute them -- pretty much the worst case scenario. Google for "secure file upload" to avoid typical mistakes.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > A question about the move_uploaded_file function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap