Jump to content

file_get_contents?


Blom

Recommended Posts

I have a simple php upload script that accepts a selected xml file's details from an external swf file, named upload.php:

 

<?PHP
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']); 

move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path);
echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";
echo file_get_contents($_FILES['Filedata']['tmp_name']);

?>

 

It uploads the file fine, and outputs the first echo, however not the second. I get this in an errorlog file:

 

[Date & Time] PHP Warning:  file_get_contents(/tmp/phpTNBGAa) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: 
No such file or directory in /home/ustudios/public_html/blommestein/test/upload.php on line 7

 

I'm new to php. Any idea of how to get what I'm trying to achieve, that being to echo the uploaded xml file's contents?

 

Thanks.  :)

 

Link to comment
Share on other sites

I have a simple php upload script that accepts a selected xml file's details from an external swf file, named upload.php:

 

<?PHP
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']); 

move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path);
echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";

//TRY CHANGING BELOW LINE
echo file_get_contents($_FILES['Filedata']['tmp_name']);
//TO THIS
$contents = file_get_contents($target_path);
echo $contents;
?>

 

It uploads the file fine, and outputs the first echo, however not the second. I get this in an errorlog file:

 

[Date & Time] PHP Warning:  file_get_contents(/tmp/phpTNBGAa) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: 
No such file or directory in /home/ustudios/public_html/blommestein/test/upload.php on line 7

 

I'm new to php. Any idea of how to get what I'm trying to achieve, that being to echo the uploaded xml file's contents?

 

Thanks.  :)

 

Link to comment
Share on other sites

Thanks for the response.

 

I no longer get the errorlog, but the second is still a blank echo.

 

Try for yourself if you like (if you have an xml file handy, or you could use this http://ustudios.net/blommestein/test/uploads/test.xml ).

 

http://ustudios.net/blommestein/test/Test.swf

 

One more thing, would it be possible to get a file's contents without actually storing it in a directory? That ha dbeen my original intent, I just added the file upload to make sure the $_FILES data was actually being sent.

 

Thanks.  :)

Link to comment
Share on other sites

No it still does the same thing as before, thanks though.

 

What I'm really trying to do is simply echo a chosen xml files contents out, without uploading the xml file to a directory. Is that at all possible?

Link to comment
Share on other sites

Once you move the file it is no longer in the tmp directory. You would need to use....

 

echo file_get_contents($target_path . basename( $_FILES['Filedata']['name']));

 

What I'm really trying to do is simply echo a chosen xml files contents out, without uploading the xml file to a directory. Is that at all possible?

 

If the file is accessible via the network.

Link to comment
Share on other sites

You would need to use....

 

echo file_get_contents($target_path . basename( $_FILES['Filedata']['name']));

 

Are you sure? Because I don't know if you noticed:

 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['Filedata']['name']);

 

 

But here's the complete script, uploading, echoing "the file <filename> has been uploaded", but not echoing the xml file contents:

<?PHP
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['Filedata']['name']); 

move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path);
echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";

$contents = file_get_contents($target_path);
echo $contents;
?>

 

What I'm really trying to do is simply echo a chosen xml files contents out, without uploading the xml file to a directory. Is that at all possible?

 

What I meant was to echo the contents out without moving it from the temp directory to a directory on my server. Is that possible?

 

By the way good track list whoever uploaded tracks.xml. :)

Link to comment
Share on other sites

<?PHP

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['Filedata']['name']);

 

if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path)) {

echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";

$contents = file_get_contents($target_path);

print_l ($contents);

} else {

  echo "Error Uploading File.";

}

 

?>

Link to comment
Share on other sites

<?PHP

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['Filedata']['name']);

 

if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $target_path)) {

echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";

$contents = file_get_contents($target_path);

print_l ($contents);

} else {

  echo "Error Uploading File.";

}

 

?>

 

Nope still does the same as the others. Then again I dont see why changing an echo statement to a print statement would help.

 

Thanks anyway though.

Link to comment
Share on other sites

Wait a second I just noticed something. Perhaps because the upload is being done from flash, but when I echo basename( $_FILES['Filedata']['name']) or any other part of the filedata array, it comes as blank, however it DOES upload.

 

That is why

 

echo "The file ". basename( $_FILES['Filedata']['name']). " has been uploaded";

 

actually comes as simply "The file has been uploaded."

 

Could that be part of the problem? If so what the hell is going on?

Link to comment
Share on other sites

Try removing the basename() from your $target_path.  I think it is meant for when you specify a real path to a real file that already exists, not for something that is in the FILES array

 

EXAMPLE FROM THE MANUAL

$path = "/home/httpd/html/index.php";
$file = basename($path); 

 

I dunno, just came across that so maybe it is throwing things off.  If basename can't operate on something from the files array then that would explain why your echo statement is blank for content.  Not sure why it would still do move_uploaded file, but it's something to try anyway.

Link to comment
Share on other sites

What I'm really trying to do is simply echo a chosen xml files contents out, without uploading the xml file to a directory. Is that at all possible?

 

Unless you are on the same network, like Thorpe said, you will have to upload the file, however, you don't have to store it (i.e. use move_uploaded_file to move it to the uploads directory).  Use use file_get_contents on the tmpname...

 

echo file_get_contents($_FILES['Filedata']['tmpname']);

 

The file will be destroyed once the script finishes execution.

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.