0

I'm a noob in php and I need some help please. I have a uploading script that uploads something to my server. So after I upload the file i get a message your file was upload and I want this message to be posted in my html upload page(main page) and the code is like this:

 if(empty($errors)===true){ move_uploaded_file($file_tmp,"upload/".$file_name); echo "Your file was upload!"; <- I want this line to be printed in other page // }else{ print_r($errors); } } 

Edit : And I found an error to my script if you could help me with this two please:

$file_name=$_FILES['file']['name']; $file_tmp =$_FILES['file']['tmp_name']; $file_type=$_FILES['file']['type']; $file_ext=strtolower(end(explode('.',$_FILES['file']['name']))); $extensions = array("rar","zip","jpeg","jpg","png","gif"); if(in_array($file_ext,$extensions)=== false){ $error[]= "Extension not allowed, please choose a RAR or ZIP file or if you upload an image use JPEG, JPG, PNG or GIF format.</br> Thank you!"; } 

This is my code to restrict file upload,but it won't take the restrictions,can any1 tell me why please?

Hope I'm explicit enough. Thank you !

1
  • Try to use session to print your message anywhere in html.first store that message in variable than dispaly in proper place. Commented May 17, 2013 at 4:01

2 Answers 2

1

You can modify your script in the below way.

... if(empty($errors)===true){ move_uploaded_file($file_tmp,"upload/".$file_name); header("Location: otherpage.php?msg=success"); }else{ header("Location: otherpage.php?msg=failure"); print_r($errors); } } ... 

In otherpage.php,

if(isset($_GET['msg']) && $_GET['msg'] == 'success') { echo "File uploaded sucessfully"; } 

EDIT:

You have written print_r($errors). I am assuming that $errors is an array. In that case you can pass this variable to the url by using json_encode($errors), see below.

$err = json_encode($errors); $urlEncode = urlencode($err); //now pass this to the url like this header("Location: otherpage.php?msg=failure&err=".$urlEncode); //in otherpage.php, you have to decode it. if(isset($_GET['err'])) { $errDecode = urldecode($_GET['err']); $err = json_decode($errDecode); print_r($err); //prints the error } 

Extensions:

Your script seems to be correct, can you try it in the below way.

$extensions = array("rar","zip","jpeg","jpg","png","gif"); if(!in_array($file_ext,$extensions)){ $error[]= "your error statement"; } 
Sign up to request clarification or add additional context in comments.

3 Comments

Try'd it both way's.it won't take my exceptions. It will still upload any file i give him... and thank you a lot for the error message part that one worked!! :)
Great...Ok for the extensions, first try to echo the extension, that is echo the variable $file_ext. See if its coming correctly and without a dot in front.
Done it thank you a lot!!! now the only problem i have to sovle out is why it gives me "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\InstantUpload\fail.php on line 3 " instead of my defined error statment, wich line 3 is : $error=array();
0

Place your echo on the page you want it to appear on, not on a separate page. Alternately send your echo string as part of the form submission, but this is probably not appropriate for this case. But then you could set what should be printed on the next page, but set it in advance.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.