[Solved] Script fails to interact with an upload form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frozenarmageddon
Forum Newbie
Posts: 19
Joined: Wed Aug 05, 2009 6:29 pm

[Solved] Script fails to interact with an upload form

Post by frozenarmageddon »

Hello, I am really new to PHP and still learning off tutorials.
I copied a source code from a tutorial and fixed it to work without errors, but now it just doesn't do what its supposed to.
It is a completely basic upload form, you can see my failing result [with all the debug messages] here: http://frozenarmageddon.orgfree.com/upl ... loads2.php
I replaced a lot of stuff that didn't work for some reason with much simpler stuff that worked, But because it is 3:30 AM here I accidentally deleted my copy of the tutorial's source, so I can't show you what it was before I changed it.

Source [Sorry it seems like it won't let me upload it.]:

Code: Select all

 
<html>
<body>
<form enctype="multipart/form-data" action="" method="post">
Choose a file: <input name="file_name" type="file" />
<input type="submit" value="Upload" name="submit" />
</form>
</body>
</html>
<?php
 session_start();
 echo "DEBUG: Season start <br/>";  //DEBUG MESSAGE!!
 if(!isset($_POST['submit']))
 {
 echo "DEBUG: If submit <br/>";  //DEBUG MESSAGE!!
 echo "";
 }
 else { 
 echo "DEBUG: Elese submit <br/>";  //DEBUG MESSAGE!!
 $filename = $_FILES['file']['name'];
 echo 'DEBUG: Variable <b><em>$filename</em></b> set to: '.$filename."<br/>";  //DEBUG MESSAGE!!
 $filesize = $_FILES['file']['size'];
 echo 'DEBUG: Variable <b><em>$filesize</em></b> set to: '.$filesize."<br/>";  //DEBUG MESSAGE!!
 $tmpname_file = $_FILES['file']['tmp_name'];
 echo 'DEBUG: Variable <b><em>$tmpname_file</em></b> set to: '.$tmpname_file."<br/>";  //DEBUG MESSAGE!!
   
 if($filesize > ‘1000000?) {
 echo "Way too big!!";
 } else {
 move_uploaded_file($tmpname_file, "..");
 echo "Successful.<br /><b>URL: </b>http://frozenarmageddon.orgfree.com/uploads/".$filename."</textarea>";
 }
}
?> 
 
 
 
and it results in [Uploaded file called 124723746384.jpg]:
 

Code: Select all

 
DEBUG: Season start
DEBUG: Else submit
DEBUG: Variable $filename set to:
DEBUG: Variable $filesize set to:
DEBUG: Variable $tmpname_file set to:
Successful.
URL: http://frozenarmageddon.orgfree.com/uploads/
 
Ofcourse it didn't upload anything, and I checked the variable passing, its OK. so the problem must be in the interaction with the Form... or just me $#!^ing up somewhere else :|

In any case, sorry for the not so clean post, as I mentioned before, its 3 AM here [3:43 at exactly this moment] and the only thing that keeps me awake is Blink182. Also I think I got a ROOTkit again. Tomorrow if there won't be any reply I am going to clean this post a bit, I just wanted to get over with it for today and go to sleep "^^
Last edited by frozenarmageddon on Fri Aug 07, 2009 4:09 pm, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Script fails to interact with an upload form

Post by aceconcepts »

frozenarmageddon
Forum Newbie
Posts: 19
Joined: Wed Aug 05, 2009 6:29 pm

Re: Script fails to interact with an upload form

Post by frozenarmageddon »

Thanks, both of you.
aceconcepts wrote:Take a look at: http://www.tizag.com/phpT/fileupload.php
Because it describes an other way I will read it later to learn from it, but at the moment I am going to try to fix what I already got.
McInfo wrote:Having error_reporting and display_errors enabled will allow you to see the following errors
Thanks, I will see what I can do about it.

Now to bug fixing...
I fixed what I understood, but because I can't turn on error_reporting and display_errors [at least not at the moment] I don't know of any other errors.
anyway, everything seems to be fine, it just won't upload :O

The fixed code:

Code: Select all

 
<html>
<body>
<form enctype="multipart/form-data" action="" method="post">
Choose a file: <input name="file_name" type="file" />
<input type="submit" value="Upload" name="submit" />
</form>
<?php
 session_start();
 echo "DEBUG: Season start <br/>";  //DEBUG MESSAGE!!
 if(!isset($_POST['submit']))
 {
 echo "DEBUG: If submit <br/>";  //DEBUG MESSAGE!!
 echo "";
 }
 else { 
 echo "DEBUG: Elese submit <br/>";  //DEBUG MESSAGE!!
 $filename = $_FILES['file_name']['name'];
 echo 'DEBUG: Variable <b><em>$filename</em></b> set to: '.$filename."<br/>";  //DEBUG MESSAGE!!
 $filesize = $_FILES['file_name']['size'];
 echo 'DEBUG: Variable <b><em>$filesize</em></b> set to: '.$filesize."<br/>";  //DEBUG MESSAGE!!
 $tmpname_file = $_FILES['file_name']['tmp_name'];
 echo 'DEBUG: Variable <b><em>$tmpname_file</em></b> set to: '.$tmpname_file."<br/>";  //DEBUG MESSAGE!!
   
 if($filesize > "1000000") {
 echo "Way too big!!";
 } else {
 move_uploaded_file($tmpname_file, "uploads/");
 echo "Successful.<br /><b>URL: </b>http://frozenarmageddon.orgfree.com/uploads/".$filename."</textarea>";
 }
}
?>
</body>
</html>
 
Last edited by frozenarmageddon on Fri Aug 07, 2009 4:08 pm, edited 1 time in total.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Script fails to interact with an upload form

Post by jackpf »

Why can't you turnon errors? :?
frozenarmageddon
Forum Newbie
Posts: 19
Joined: Wed Aug 05, 2009 6:29 pm

Re: Script fails to interact with an upload form

Post by frozenarmageddon »

McInfo wrote:
frozenarmageddon wrote:The fixed code:
session_start() is still not at the beginning of the script where it is supposed to be.

"uploads/" is not a valid destination. See the manual page for move_uploaded_file(). The destination should be the path to a file, not a directory.

You can turn on error reporting at runtime with the following two statements, which you should have read about in the manual page I linked to in a previous post.

Code: Select all

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
?>
I did so many noobish mistakes, I guess its pretty clear I just started with PHP "^^
Thanks for the help. about the session_start(), It was at the beginning before I moved it back down, sorry for the confusion :o
and about both manuals - I am sorry, I read both, but because it was like a few minutes after I woke up I think I missed a few important lines :O

-Edit:-
Ok, I fixed everything and now everything works, thanks guys :D

To fix it, I removed the whole season_start() part,
added the file's current location after "action=",
and of course changed the move_uploaded_file to:

Code: Select all

move_uploaded_file($tmpname_file, "uploads/$filename");
(Because in one of the version I wrote it as

Code: Select all

move_uploaded_file($tmpname_file, "uploads/file.php");
which basically made it write to a php file :o

The fixed [and polished and slightly censored :O] code:

Code: Select all

 
<html>
<body>
<form enctype="multipart/form-data" action="http://------/upload.php" method="post">
Choose a file: <input name="file_name" type="file" />
<input type="submit" value="Upload" name="submit" />
</form>
<?php
 if(!isset($_POST['submit']))
 {
 echo "Choose a file to upload, and click Upload :D";
 }
 else { 
 $filename = $_FILES['file_name']['name'];
 $filesize = $_FILES['file_name']['size'];
 $tmpname_file = $_FILES['file_name']['tmp_name'];
   
 if($filesize > "1000000") {
 echo "Way too big!!";
 } else {
 move_uploaded_file($tmpname_file, "uploads/$filename");
 echo "Successful.<br /><b>URL: </b><a href=http://------/uploads/".$filename.">http://------/uploads/".$filename."</a></textarea>";
 }
}
?>
</body>
</html>
 
[sorry for the censors, I don't want someone "accidantly" using it as an upload site while its still not password protected]
Post Reply