Jump to content

Where did the file go to? And a few other silly questions.....


dicky96

Recommended Posts

Hi everyone

 

OK I'm only learning php at the moment, working from various online tutorials, so this is probably dumb.

 

I'm having problems at the moment uploading a file from one php page to another (all running on my local PC at the moment)

 

The page that sends the file seems to work OK

<html>

<head>
<title>upload</title>
</head>

<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">filename:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="submit" />
<br/>
</form>
</body>

</html>

 

The php page that receives the file is like this:  (a load of it is commented out as I've spent since yesterday afternoon trying to figure this problem for myself)  :-[

 

<html>
<head>
</head>
<body>
<?php
if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br/>";
    echo "Type: " . $_FILES["file"]["type"] . "<br/>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb" . "<br/>";
    echo "Temp File: " . $_FILES["file"]["tmp_name"]."<br/>";
    }

echo file_exists("c:/temp/upload/".$_FILES["file"]["tmp_name"]);
//    {
//   echo $_FILES["file"]["name"]." already exists. ";
//    }
//else
//    {
//    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); 
//    echo "Stored In: " . "upload/" . $_FILES["file"]["name"].<br/>;
//    echo $a; 
    echo " yes!!";
//    }
?>
</body>
</html>

 

Using a test file, this produces the following output in my browser:

 

 

Upload: DSCF0026_.JPG

Type: image/pjpeg

Size: 36.705078125 Kb

Temp File: C:\Temp\upload\php21.tmp

yes!!

 

My question is what happens to the file?

 

This line

 

echo file_exists("c:/temp/upload/".$_FILES["file"]["tmp_name"]);

 

does not echo any value.  Why is that?

 

Also if I look in the upload folder, no file appears

 

A couple other silly questions if it's OK to add here..... half the time in PHP path's seem to use / and the other half seem to use \  so I've seen C:/program files/......  and C:\program files\.....  in various places in php.ini and in sample code.  Why does this change and how do you know where to use each?

 

Other silly question  -  if I add echo error_reporting() it tells me it set to 6143 - but where do all the error reports go to?  When I make mistakes in my script like leaving off a " or a ; I don't get any error messages other than

 

-----------------------------------------------------

 

The page cannot be displayed

There is a problem with the page you are trying to reach and it cannot be displayed.

 

--------------------------------------------------------------------------------

 

Please try the following:

 

Open the localhost home page, and then look for links to the information you want.

Click the  Refresh button, or try again later.

 

Click  Search to look for information on the Internet.

You can also see a list of related sites.

 

HTTP 500 - Internal server error

Internet Explorer 

-----------------------------------------------------------------------

 

TIA

dicky

 

 

 

Link to comment
Share on other sites

This line

 

echo file_exists("c:/temp/upload/".$_FILES["file"]["tmp_name"]);

 

does not echo any value.  Why is that?

file_exists returns a boolean value (TRUE or FALSE) use var_dump to see what it returns

 

Also if I look in the upload folder, no file appears

By default all file uploads get uploaded to Windows Temp folder (C:/WINDOWS/Temp) temporarily I believe. If you want to PHP to upload temporary files to another location the setup the upload_tmp_dir direcitive within the php.ini eg:

upload_tmp_dir  = "C:/file_uploads";

Create a folder called file_uploads within the root of your C:/ drive and files will get uploaded temporarily there. PHP will clear temporary files after the script has finished being parsed.

 

Also you'll need to uncomment the following line:

//    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

In order for your file to be moved to the uploads/ folder permanently. As you are using a relative path for the above function PHP will uploaded file into the upload folder from where your script is, eg if your script is in C:\htdocs\ then it'll upload the file to C:\htdocs\upload\

 

A couple other silly questions if it's OK to add here..... half the time in PHP path's seem to use / and the other half seem to use \  so I've seen C:/program files/......  and C:\program files\.....  in various places in php.ini and in sample code.  Why does this change and how do you know where to use each?

It does not matter what you use. However for Windows people say you should use \\ rather than \ ((yes two backslashes, eg C:\\WINDOWS\\Temp) for file paths. However I prefer to use forward slashes. There is no difference between.

 

Other silly question  -  if I add echo error_reporting() it tells me it set to 6143 - but where do all the error reports go to?  When I make mistakes in my script like leaving off a " or a ;

Make sure you have enabled the display_errors directive within the php.ini in order to see errors during runtime. Otherwise your errors will get logged to your servers error log and/or Windows error log. You can make PHP log errors to a dedicated error log by setting up the error_log directive within the php.ini too, eg:

error_log = "C:/php_errors.log";

Create a file called php_errors.log within the root of the C: drive. All errors will be saved to that error log.

 

Also to disable the error page IE shows go to Tools (Hold down Alt key to see the toolbar in IE7) > Internet Options > Advanced Tab and disable a setting called Show friendly HTTP error messages. Click OK to close remaining open windows.

 

NOTE: If you make any changes via your php.ini be sure to always save your php.ini (obviously) and to restart your server software (eg: Apache, IIS etc)

Link to comment
Share on other sites

Hi Wildteen  :)

 

First of all thanks for taking the time to help, but hey you guessed it! I now have more questions, lol

 

I did say I am only learning and had taken a day trying to figure this out so.... In that time I worked a few things out....

 

I agree that the temporary file goes into the windows temp folder by default - because that is exactly what happened when I accidentally redirected in the php.ini file to a folder that did not exist... but now I have

 

upload_tmp_dir="c:\tmp\upload"  (which does exist)

 

in my php.ini file  (and i have searched for any more upload_tmp_dir in the file just in case there was more than one)

 

I commented out the move_uploaded_files stuff as my script didn't seem to be getting that far.

 

and yet I still can't see where that file goes to

 

Also thanks for explaining what the short path "/upload" means as that was confusing me.  As my php file is running in htdocs/test, would I have to create a folder htdocs/test/upload or will php do that automatically if it does not already exist?

 

 

I'm still not sure about this \ and / business though, I appreciate you say they are interchangeable but when I edit the line

 

echo file_exists("c:/temp/upload/".$_FILES["file"]["tmp_name"]);

 

and replace it with

 

echo file_exists("c:\temp\upload\".$_FILES["file"]["tmp_name"]);

 

then I get one of those "page can not be displayed" error messages, so why is that?

 

best regards

dicky

Link to comment
Share on other sites

Also thanks for explaining what the short path "/upload" means as that was confusing me.  As my php file is running in htdocs/test, would I have to create a folder htdocs/test/upload or will php do that automatically if it does not already exist?

Yes you will need to create the folder htdocs/test/upload

 

I'm still not sure about this \ and / business though, I appreciate you say they are interchangeable but when I edit the line

 

echo file_exists("c:/temp/upload/".$_FILES["file"]["tmp_name"]);

 

and replace it with

 

echo file_exists("c:\temp\upload\".$_FILES["file"]["tmp_name"]);

Argh sorry. This bit I forgot to mention. When using a backslash (\) in your scripts always escape them unless you are escaping another character, so this line should be:

echo file_exists("c:\\temp\\upload\\".$_FILES["file"]["tmp_name"]);

Have a read of the following about escape sequences

 

Link to comment
Share on other sites

Hi again

thanks for the helps - some of my questions are now sorted:

 

/ and \ = Sorted!!  (thanks for the info on escape sequences)

 

error reporting = Sorted!!  (I had display_errors = off in php.ini)

 

The echo file_exists(.......)  I got from www.w3schools.com/php/func_filesystem_file_exists.asp I assume this is an error in their documentation?

 

I've now modified my code to use var_dump like this:

 

<?php
$a=0;
echo error_reporting()."<br/>";
if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br/>";
    echo "Type: " . $_FILES["file"]["type"] . "<br/>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb" . "<br/>";
    echo "Temp File: " . $_FILES["file"]["tmp_name"]."<br/>";
    }

$a=file_exists("c:/Temp/upload/".$_FILES["file"]["tmp_name"]);
var_dump($a);
?>

 

 

When I now test this I get the following output

 

6143

Upload: DSCF0026_.JPG

Type: image/pjpeg

Size: 36.705078125 Kb

Temp File: C:\Temp\upload\php1C.tmp

bool(false)

 

The folder C:/Temp/upload does exist

so now I'm back to my first question..... where did the file go?  I can't use move_uploaded_file() as it does not seem to be there though

 

echo "Temp File: " . $_FILES["file"]["tmp_name"]."

 

reports that it is!

 

best regards

dicky

 

 

Link to comment
Share on other sites

The line move_uploaded_file is the most important line, as it is what actually copies the file from the temp dir to the perm dir.

 

<?php

if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] ;
}
else
{
    
   echo "Upload: " . $_FILES["file"]["name"].'<br>';
    echo "Type: " . $_FILES["file"]["type"].'<br>';
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb".'<br>';
    echo "Temp File: " . $_FILES["file"]["tmp_name"].'<br>';



$filename = $_FILES["file"]["name"];
$uploaddir = '/path/to/your/upload/directory/'; // change this to your path
    $uploaddir.= $filename;
   
    //Copy the file to some permanent location
    if(move_uploaded_file($_FILES["file"]["tmp_name"], $uploaddir))
    {
      echo "File Updated!";
    }
    else
    {
      echo "There was a problem when uploding the new file";
    }

}

?>

 

Try that and let us know what you get.

Link to comment
Share on other sites

Hi Chronister

 

Your code works correctly, as does mine now  ;D

 

<?php
$a=0;
echo error_reporting()."<br/>";
if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br/>";
    echo "Type: " . $_FILES["file"]["type"] . "<br/>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb" . "<br/>";
    echo "Temp File: " . $_FILES["file"]["tmp_name"]."<br/>";
    }

$a=file_exists("c:/Temp/upload/".$_FILES["file"]["tmp_name"]);
var_dump($a);
echo "<br/>";

if (file_exists("c:/Program Files/Apache Group/Apache2/htdocs/test/upload/".$_FILES["file"]["name"]))
    {
    echo $_FILES["file"]["name"]." already exists.";
    }
else
    {
    move_uploaded_file($_FILES["file"]["tmp_name"],"c:/Program Files/Apache Group/Apache2/htdocs/test/upload/".$_FILES["file"]["name"]); 
    echo "Permanent file Stored In: "."upload/".$_FILES["file"]["name"]."<br/>";
    $a=file_exists("upload/".$_FILES["file"]["name"]);
    var_dump($a);
    }
?>

 

gives the result:

 

6143

Upload: DSCF0038_.JPG

Type: image/pjpeg

Size: 98.869140625 Kb

Temp File: C:\Temp\upload\php39.tmp

bool(false)

Permanent file Stored In: upload/DSCF0038_.JPG

bool(true)

 

 

I noticed that the file_exists on the temp file still returns (false) so this was kind of a red herring.

 

Thanks for the help

dicky

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.