Hello List.

I am learning php and MySQL for my project work.

I am trying to upload files to local server.

First, a page is displayed on the browser to upload file

Relevant code:

<form method="post" action="index.php" enctype="multipart/form-data">
<input type="file" name="file">
<br>
<input type="submit" name="submit" value="upload">
</form>
After the user chooses a file and hit the submit button,the browser displays the content from the "uploads" directory.

Php code:

<?php
         if (isset ($_POST['submit']) && ($_POST['submit']=="upload")) {
            $pathname = "uploads";
            if (!file_exists($pathname)) {
                $mkdir = mkdir($pathname);
                if (!$mkdir) {
                    echo 'could not create directory uploads/';
                    exit();
                }
            }
            $filename = $_FILES['file']['tmp_name'];
$destination = "uploads/" . time() . "_-" . $_FILES['file']['name'];


          move_uploaded_file($filename, $destination);
header("Location:uploads/");
But the uploads directory is empty and no warning or error is displayed !

Then i set a check for the return value of move-uploaded_file.
if(isset ($move_uploaded_file)):
               echo ""success;
           else:
               echo "error in uploading";
           endif;
But it does not print anything ! .According to manual,

Returns *TRUE* on success.

If /filename/ is not a valid upload file, then no action will occur, and *move_uploaded_file()* will return *FALSE*.

If /filename/ is a valid upload file, but cannot be moved for some reason, no action will occur, and *move_uploaded_file()* will return *FALSE*. Additionally, a warning will be issued.

So the above code should have printed "error in uploading".
Then i tried the copy() function and it prints a warning:

*Warning*: copy() [function.copy <http://localhost/file_uploading/function.copy>]: Filename cannot be empty in */srv/www/htdocs/file_uploading/index.php* on line *38*
line 38 is

copy($filename, $destination);
Then i did a print_r($_FILES['file']) and found that $_FILES['file']['error'] was 1 .The problem was the file i was trying to upload was 6M in size,greater than upload_max_filesize in /etc/php.ini which was 2M.I changed that and now it works.

So I want to know - does move_uploaded_file returns false on failure or is there a bug in my code ?

I am using php 5.3.8 which is configured with

'./configure' '--prefix=/php' '--with-apxs2=/apache/bin/apxs' '--with-config-file-path=/etc' '--with-mysql-sock=/tmp/mysql.sock' '--with-openssl' '--with-zlib' '--enable-bcmath' '--with-bz2' '--enable-calendar' '--with-gdbm' '--with-enchant' '--enable-exif' '--enable-ftp' '--with-gd' '--enable-gd-native-ttf' '--with-gettext' '--with-gmp' '--enable-mbstring' '--with-mcrypt' '--with-readline' '--enable-soap' '--enable-sqlite-utf8' '--enable-zip' '--with-mysql=mysqlnd' '--with-mysqli=mysqlnd' '--with-jpeg-dir=/usr/lib' '--with-freetype-dir'
and error_reporting directive in php.ini is "error_reporting = E_ALL | E_STRICT".

Reply via email to