Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,689 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,320 people online right now. Registration is fast and FREE... Join Now!




PHP server side, POST

 
Reply to this topicStart new topic

PHP server side, POST

nombre
post 7 Jun, 2008 - 05:03 AM
Post #1


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

I have this PHP code that I'm trying to get to write a file that it receives from a POST form a Java app:
CODE
<?php
// In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead
// of $_FILES.

$uploaddir = '/www/';
$uploadfile = $uploaddir . basename($_FILES["fileToUpload"]["name"]);
$file_name = "image.jpg";

//move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile);
if ($_FILES["fileToUpload"]["error"] > 0)
    {
    echo "Apologies, an error has occurred.";
    echo "Error Code: " . $_FILES["fileToUpload"]["error"];
    }
else
    {
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
      "/" . $_FILES["fileToUpload"]["name"]);
      echo "Stored in: " . "" . $_FILES["file"]["name"];
      }

$handle = fopen($file_name, "w");
        fputs($handle, $_FILES["fileToUpload"]["name"]);
        fclose($handle);

  



print_r($_FILES);



?>


The output says that it is saving to Array. How do I get that data from Array to file?

Here is the output from the Java side (client)
Stored in: Array
(
[IMG_1909_jpg] => Array
(
[name] => IMG_1909.jpg
[type] => application/octet-stream
[tmp_name] => C:\wamp\tmp\phpD912.tmp
[error] => 0
[size] => 123231
)

)

Could someone please help? Thanks
User is offlineProfile CardPM

Go to the top of the page

pertheusual
post 7 Jun, 2008 - 08:45 AM
Post #2


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 230



Thanked 2 times
My Contributions


Ok, the first thing you need to realized is that "fileToUpload" isn't correct. You have to use the name that is being transmitted by the Java application, which in this case is "IMG_1909_jpg" according to the print_r output. Secondly, it isn't saying "Stored in: Array", the "Array" comes from the print_r, you just don't have a new line separating them.

You should also make sure yo check the output of move_uploaded_file.


Like this:
php

<?php
//YOU should make the java code POST using a standardized name
$postVariable = "IMG_1909_jpg"

$uploaddir = '/www/';
$uploadfile = $uploaddir . basename($_FILES[$postVariable]["name"]);
$file_name = "image.jpg";

if ($_FILES[$postVariable]["error"] > 0 || !move_uploaded_file($_FILES[$postVariable]["tmp_name"], $uploadfile))
{
echo "Apologies, an error has occurred.";
if(isset($_FILES[$postVariable]["error"])) echo "Error Code: " . $_FILES[$postVariable]["error"];
}
else
{
echo "Stored in: " . "" . $_FILES[$postVariable]["name"];
}

$handle = fopen($file_name, "w");
fputs($handle, $_FILES[$postVariable]["name"]);
fclose($handle);

?>



Also, why are you writing the filename into "image.jpg"? That doesn't really make sense...
What is that fputs supposed to be doing?

You are also attempting to save the file into "/www/", which is probably wrong. You should make a subdirectory for uploaded files and put them in there. Make sure that you set the folder permissions so that PHP can write the file successfully, otherwise it will give you an error every time.

Per
User is offlineProfile CardPM

Go to the top of the page

nombre
post 7 Jun, 2008 - 07:52 PM
Post #3


New D.I.C Head

*
Joined: 7 Jun, 2008
Posts: 49

Thanks Per I finally got it working. Is there any way to change the filename to image.jpg after its done? Thanks again this is my second day with php smile.gif.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 7 Jun, 2008 - 07:58 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,354



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


CODE

//syntax : rename source destination
<?php
  rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>


As per php.net
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:36AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month