logo
Apache Lounge
Webmasters

 


About

Forum Index Downloads Search Register Log in  RSS Apache Lounge
 



Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Apache Lounge is not sponsored.

Your donations will help to keep this site alive and well, and continuing building binaries.



File Uploading failure via PHP

 
Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner



View previous topic :: View next topic  
Author Message
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Sun 14 Feb '16 15:48    Post subject: File Uploading failure via PHP Reply with quote

Hello

My computer spec: Window XP 32 bit Home, Apache 2.2, PHP 5.3.5.
When uploading the file, always, failure are occurred: there is no file under c:\windows\temp.

Simple PHP Code:

if($_POST['submit']) {

$name = $_FILES["upload"]["name"];
$temp = $_FILES["upload"]["tmp_name"];
$type = $_FILES["upload"]["type"];
$size = $_FILES["upload"]["size"];

echo "$name<br>$temp<br>$type<br>$size";

if (move_uploaded_file($name,$temp)) {
echo "Uploaded sucessed<br>";
}
//echo "<img src ='$name'>";
else { echo "failed<br>";
}
}
else {

header("Location: upload_file.html");
}

paint.png supposed to be uploaded to apache server
Output of html: paint.png
c:\windows\temp\php132.tmp
image/x-png
67213 failed

as shown in output, return value of move_uploaded_file is 0 (which is failure.)

I checked php.ini/httpds.conf for file uploading configuration. it seems like correctly setting up.

Should I increase connection timeout time from apache?
If yes, how can timeout time be increased?
Is it related to my PC itself?

Your support would be appreciated.

Thanks,
davidapache
Back to top
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Tue 16 Feb '16 4:07    Post subject: Reply with quote

Hello Admin

Please would you review my inquiry, whenever you have a chance.

Thanks,
davidapache
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Tue 16 Feb '16 7:16    Post subject: Reply with quote

Hello David, it looks like you may be looking for the information that is here:
http://php.net/manual/en/features.file-upload.post-method.php

The code below may be relevant to you problem. Only guessing though, since you omitted the input part of your code.
Quote:
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
Back to top
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Wed 17 Feb '16 4:03    Post subject: Reply with quote

Hello Wait

Adding html file calling php file.
<html xmlns = "http://www.w3.org/
1999/xhtml" xml:lang = "en"
lang = "en">

<head>
<meta http-equiv="Content-Type"
content = "text/html;
charset= utf-8" />
<title>Uploading an Image </title>
<style type="text/css" title="text/css"
media = "all">
.error {
font-weight:bold
color:#C00;
}
</style>
</head>
<body>

<h1>Uploading Files</h1>
<hr>
<form action = "upload_file.php" method= "post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="90000" />
File: <input type="file" name = "upload">
<input type = "submit" name= "submit" value ="Uploading File Now">

</form>
</body>
</html>

Total file size is about 68k. But still the same failure is occurred.

Please let me know if you get the same error.

javascript:emoticon('Embarassed')

Thanks,
David
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Wed 17 Feb '16 4:33    Post subject: Reply with quote

Hello David, I would be happy to try it.

I'm still not sure what your setup is though:
1) The html file and php files are both on a remote server?
2) OR The server and php are both on your PC, and you are doing everything from your pc, there is no remote server? This I cannot try.

Just in case, are you testing '$_FILES['userfile']['error']' somewhere? If this is what is giving you a value of '0', it means that there are no errors:
http://php.net/manual/en/features.file-upload.errors.php

The setup below worked in Firefox 41.0 and IE8 on Windows xp. Both the html file and php were on a remote server, together in public_html/ directory. Hope it helps!

Output displayed by receiver.php:
Quote:
File received ok
File name: test.PNG
File type: image/png
File size: 388

PHP File:
Code:
<?php
// File name: receiver.php
// http://php.net/manual/en/features.file-upload.post-method.php
// http://php.net/manual/en/features.file-upload.errors.php

if(isset($_FILES)){
    $num_error = $_FILES['userfile']['error'];
    if($num_error == 0){
        echo 'File received ok <br>';
        echo 'File name: '.$_FILES['userfile']['name'].'<br>';
        echo 'File type: '.$_FILES['userfile']['type'].'<br>';
        echo 'File size: '.$_FILES['userfile']['size'].'<br>';
    }
    else{
        echo '$_FILES Error: '.$num_error.'<br>';
        echo get_error_text($num_error);
    }
}
else echo 'Did not receive $_FILES variable';
   
function get_error_text($num){
    $str_arr = array();
    $str_arr[] = '';
    $str_arr[] = 'The uploaded file exceeds the upload_max_filesize<br>
                  directive in php.ini.';
    $str_arr[] = 'The uploaded file exceeds the MAX_FILE_SIZE<br>
                  directive that was specified in the HTML form.';
    $str_arr[] = 'The uploaded file was only partially uploaded.';
    $str_arr[] = 'No file was uploaded.';
    $str_arr[] = '';
    $str_arr[] = 'Missing a temporary folder. Introduced in PHP 5.0.3.';
    $str_arr[] = 'Failed to write file to disk.Introduced in PHP 5.1.0.';
    $str_arr[] = 'A PHP extension stopped the file upload. PHP does<br>
                  not provide a way to ascertain which extension caused<br>
                  the file upload to stop; examining the list of loaded<br>
                  extensions with phpinfo() may help. Introduced in<br>
                  PHP 5.2.0.';
            
    return $str_arr[$num];
}
?>

HTML File:
Code:
<?php
// This file name: upload.php
$url_phpreceiver = "http://www.yourdomain.com/receiver.php";
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=charset=UTF-8">
</head>

<body>

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype = "multipart/form-data"
      action  = "<?php echo $url_phpreceiver; ?>"
      method  = "POST">
    
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
   
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>

</body>
</html>
Back to top
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Fri 19 Feb '16 5:04    Post subject: Reply with quote

Hello Wait

Set up environment: PC is window xp home version, not professional.
using http://localhost/upload.php, to make sure that file is copy to C:\Window\Temp folder as default, but

move_uploaded_file($_FILES['userfile']['name'],$_FILE['userfile']['tmp_name'])

Output:
File received ok
File name: mc.jpg
File type: image/jpeg
File size: 83503
File temp: C:\WINDOWS\Temp\php13D.tmp

However, file is not stored in C:\windows\temp\.

I heard that Liinux need to change the setting for file permission, but in the window xp (home version), it is not required.

Thanks,
Davidapache
Back to top
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Fri 19 Feb '16 5:26    Post subject: Reply with quote

Plus, warning message:

Warning move_upload_file(http://localhost/upload/paint.png);failed to open stream, No such a file or dir.
Warning: move_uploaded_file()LUnlike to move 'C:\WINDOWS\Temp\php13F.tmp to http//localhost/upload/paint.png' in C:\Apache22\docs\receiver.php on line 22
Back to top
walt



Joined: 24 Oct 2015
Posts: 25

PostPosted: Fri 19 Feb '16 5:35    Post subject: Reply with quote

Hello David, unfortunately I cannot try your setup, however, the file may be getting deleted after the script is done:
http://stackoverflow.com/questions/3817360/where-does-php-save-temporary-files-during-uploading

Also, try this search in Google:
"http://localhost/upload.php C:\WINDOWS\Temp\"

Apparently it's a common thing people have struggled with. Maybe you could enlighten me on why this is done/needed? Smile. Maybe after you have figured out what the problem is, don't want to take up your time now.

Edit
I managed to install Apache and php on my Windows xp. This example is working.

Some tweaks to the code above. In upload.php:
Code:
<?php
// This file name: upload.php
//$url_phpreceiver = "http://www.yourdomain.com/receiver.php";
$url_phpreceiver = "receiver.php";
?>
In receiver.php:
Code:
if($num_error == 0){
        echo 'File received ok <br>';
      
      $file_name = $_FILES['userfile']['name'];
        echo 'File name: '.$file_name.'<br>';
        echo 'File type: '.$_FILES['userfile']['type'].'<br>';
        echo 'File size: '.$_FILES['userfile']['size'].'<br>';
      
      $temp_name = $_FILES['userfile']['tmp_name'];
      echo 'File temp name: '.$temp_name.'<br>';
      
      rename($temp_name, "C:/Server/www/temp/".$file_name);
    }

Output:
Quote:
File received ok
File name: test.PNG
File type: image/png
File size: 388
File temp name: C:\WINDOWS\Temp\phpF.tmp

I did not have to add or change any permissions in xp. When done, there was no file in "C:\WINDOWS\Temp\".

My folders:
Code:
C:/Server
     |->Apache
     |->php
     |->www  (here are upload.php, receiver.php)
         |->temp (when done test.png is here)


I also noticed this setting in php.ini: file_uploads = On

PS I tried changing the location of the temp directory, in php.ini, to:
Code:
upload_tmp_dir = "C:/Server/www/temp/";
but it is still using "C:\WINDOWS\Temp\". Changing '/' to '\' did not work either. It would be nice to set it somewhere in the C:/Server directory.

--------------------------------------
Files used to install the server (On Windows xp sp3):

From: http://www.apachelounge.com/download/VC10/
File: httpd-2.4.18-win32.zip

From: http://www.apachelounge.com/download/additional/
File: php5apache2_4.dll-php-5.4-win32.zip

From: http://windows.php.net/download
File: VC9 x86 Thread Safe (2015-Sep-03 00:42:44)

Installation guide used for Apache and php:
http://www.devraju.com/php/installing-php-5-4-with-apache-2-4-in-32-bit-of-windows/

Guide used for installing php5apache2_4.dll:
readme.txt, In php5apache2_4.dll-php-5.4-win32.zip


Last edited by walt on Sun 21 Feb '16 4:01; edited 3 times in total
Back to top
davidapache



Joined: 08 Aug 2015
Posts: 25
Location: usa, detroit

PostPosted: Sat 20 Feb '16 19:06    Post subject: Reply with quote

Hello Wait

Finally, file was copied to destination based on your comments. I need to work more to do.

I appreciated for your support.

Thanks,
Davidapache
Back to top


Post new topic   Reply to topic    Apache Forum Index -> Coding & Scripting Corner
Page 1 of 1