Jump to content

Wondering which code to incorporate for BACK-END file.


aamberker
 Share

Recommended Posts

Hi Folks,My HTML code is -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<html><title>- File Upload Testing -</title><body><form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file" /> <br /><input type="submit" name="submit" value="Submit" /></form></body></html>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Now wondering which code to incorporate under "upload_file.php" file among the following THREE OPTIONS :)

~ OPTION ONE ~
<?phpif ($_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 "Stored in: " . $_FILES["file"]["tmp_name"]; }?>
~ OPTION TWO ~
<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)) { 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 "Stored in: " . $_FILES["file"]["tmp_name"]; } }else { echo "Invalid file"; }?>
~ OPTION THREE ~
<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_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 />"; if (file_exists("upload/" . $_FILES["file"]["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"]; } } }else { echo "Invalid file"; }?>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~1. Where exactly do I need to create "upload" folder in server??? In ROOT DIRECTRORY itself or??? :) 2. Along with "gif", "jpeg" and "pjpeg" formats [as displayed in OPTION TWO AND THREE], how may I receive the doc files as well :mellow:
Link to comment
Share on other sites

only the third option is actually saving the file on the server, first and second option only display information about uploaded file and don't save it anywherethe upload folder should be on the same folder where the upload_file.php script is located (you can change it if you want, just change in move_uploaded_file and file_exists function)to allow ms word files change this

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))

to this

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000))

Link to comment
Share on other sites

The file is initially saved to a temporary place on your server. It needs to be 'mov ed' from the temp ;ocation to its permanent spot using this line:

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

or something along those lines, before the upload is permanent. Likely why it did not appear using the first two scripts.

Link to comment
Share on other sites

only the third option is actually saving the file on the server, first and second option only display information about uploaded file and don't save it anywherethe upload folder should be on the same folder where the upload_file.php script is located (you can change it if you want, just change in move_uploaded_file and file_exists function)to allow ms word files change this
<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000))

to this

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000))

Hi Lulzim :wub:Thanks a ton for your response... Well, so the code should look as follows. Does that make sense???~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_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 />"; if (file_exists("upload/" . $_FILES["file"]["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"]; } } }else { echo "Invalid file"; }?>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Please confirm :) By the way, upon hitting the "Submit" button upon attaching the file [in xxx.html page], the next page says The page cannot be displayed :mellow: How do I get it to a "thankyou.html" page and then redirect it to our Home page after 2 seconds??? :)
Link to comment
Share on other sites

The file is initially saved to a temporary place on your server. It needs to be 'mov ed' from the temp ;ocation to its permanent spot using this line:
move_uploaded_file($_FILES["file"]["tmp_name"] . "upload/" .  $_FILES["file"]["name"]);

or something along those lines, before the upload is permanent. Likely why it did not appear using the first two scripts.

Hi jlhaslip,Where exactly do I need to place that code???Shoud I replace with this once???
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"]))
I didn't get you dude :)
Link to comment
Share on other sites

here is the version with redirect to thankyou.html

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000)){	if ($_FILES["file"]["error"] > 0)	{		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}	else	{		if (file_exists("upload/" . $_FILES["file"]["name"]))		{			echo $_FILES["file"]["name"] . " already exists. ";		}		else		{			echo "<html><head>";			echo "<meta http-equiv='Refresh' content='2; url=./thankyou.html'>";			echo "</head><body>";			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 />"; 					move_uploaded_file($_FILES["file"]["tmp_name"],			"upload/" . $_FILES["file"]["name"]);			echo "Stored in: " . "upload/" . $_FILES["file"]["name"];			echo "</body></html>";		}	}}else{	echo "Invalid file";}?>

By the way, upon hitting the "Submit" button upon attaching the file [in xxx.html page], the next page says The page cannot be displayed
make sure the filename of the php script is the same as specified on the action attribute of the form in the html file<form action="upload_file.php" method="post" enctype="multipart/form-data">in this case the file name of the above script should be upload_file.php
Link to comment
Share on other sites

here is the version with redirect to thankyou.html
<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000)){	if ($_FILES["file"]["error"] > 0)	{		echo "Return Code: " . $_FILES["file"]["error"] . "<br />";	}	else	{		if (file_exists("upload/" . $_FILES["file"]["name"]))		{			echo $_FILES["file"]["name"] . " already exists. ";		}		else		{			echo "<html><head>";			echo "<meta http-equiv='Refresh' content='2; url=./thankyou.html'>";			echo "</head><body>";			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 />"; 					move_uploaded_file($_FILES["file"]["tmp_name"],			"upload/" . $_FILES["file"]["name"]);			echo "Stored in: " . "upload/" . $_FILES["file"]["name"];			echo "</body></html>";		}	}}else{	echo "Invalid file";}?>

make sure the filename of the php script is the same as specified on the action attribute of the form in the html file<form action="upload_file.php" method="post" enctype="multipart/form-data">in this case the file name of the above script should be upload_file.php

Hey Lulzim...It should be -|| ($_FILES["file"]["type"] == "image/pjpeg")OR|| ($_FILES["file"]["type"] == "image/pjpeg"))Which is the correct once amont them??? By the way, I do not wish to change our "currentOpportunity.html" page name to "upload_file.php" :) ... I would like to retain "currentOpportunity.html" name.Upon hitting the "Submit" button after attaching .gif or .jpeg or .doc file in currentOpportunity.html page, it should head-on to a "thankyou.html" page and then redirect it to our Home page after 2 seconds...So???Heyy... By the way, jlhaslip said ~
The file is initially saved to a temporary place on your server. It needs to be 'mov ed' from the temp ;ocation to its permanent spot using this line:move_uploaded_file($_FILES["file"]["tmp_name"] . "upload/" . $_FILES["file"]["name"]);or something along those lines, before the upload is permanent. Likely why it did not appear using the first two scripts.
So??? :):mellow::wub:
Link to comment
Share on other sites

Hey... It's saying "Invalid file" :) What should I do now???
the if statement below will check if a file with same name and type already exists in the upload folder, if it does exist will show that error message (Invalid file). so if you try to upload the same file twice that message will pop out
if (file_exists("upload/" . $_FILES["file"]["name"])){echo $_FILES["file"]["name"] . " already exists. ";}

that's how you initially wrote the script, I didn't change that partif you do not want that to happen, change the file name before you move it to uploads folder.something like this:

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

where _new_file_name would be the new unique file name you generate (by using current date, time, username or whatever you want)

Link to comment
Share on other sites

Look at the if statement that causes that error to show up. One of the conditions that it's checking for is true.
What??? :) Didn't get you dude... Hey... I saw your reply message with reference to favicon.ico in my in-box but somehow I am unable to respond to your reply message. It says, your inbox is full...
Link to comment
Share on other sites

the if statement below will check if a file with same name and type already exists in the upload folder, if it does exist will show that error message (Invalid file). so if you try to upload the same file twice that message will pop out
NOPE!!!... The file was not there in “Upload” Folder… But yeah… I tried to upload the same file twice. Because, at the first attempt, it said “Invalid file” … hummmm… Check it here - http://www.mirroremage.com/testing.html ... Try to submit a file and see how it acts-up :wub:
that's how you initially wrote the script, I didn't change that partif you do not want that to happen, change the file name before you move it to uploads folder.something like this:move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . _new_file_name_);where _new_file_name would be the new unique file name you generate (by using current date, time, username or whatever you want)
Hey… Hey… HOLD ON!!!... :) I did not write that Script… :) I picked it from - http://www.w3schools.com/php/php_file_upload.asp ... :mellow: Well, as of now I have the following code in my back-end “upload_file.php” file -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000)){ if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { if (file_exists("upload/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { echo "<html><head>"; echo "<meta http-equiv='Refresh' content='2; url=./thankyou.html'>"; echo "</head><body>"; 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 />"; move_uploaded_file($_FILES["file"]["tmp_name"] . "upload/" . $_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; echo "</body></html>"; } }}else{ echo "Invalid file";}?>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~FYI – PHP Does supports - …. Pull this - http://www.mirroremage.com/test01.php Please check your inbox to know more about our Company’s website and where exactly would I wish to incorporate the above issue. The above domain is my personal domain and NOT Company's website.Now what next??? Please let me know... I would like to close this issue asap :wub:
Link to comment
Share on other sites

Hmm... shouldn't your move_uploaded_file statement look like

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

(with the first . changed to , )

Link to comment
Share on other sites

Hmm... shouldn't your move_uploaded_file statement look like
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);

(with the first . changed to , )

Hi Synook,Hey... I tried with that... It did not work :) Hence changed it to "."
Link to comment
Share on other sites

Look at the if statement that shows the message:

if ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "application/msword"))&& ($_FILES["file"]["size"] < 20000)){	...}else{	echo "Invalid file";}

If you're seeing that message, then one or more of those things is true.

Link to comment
Share on other sites

... http://au2.php.net/manual/en/function.move-uploaded-file.phpPHP is a logical language, having a period delimiter in the move_uploaded_file function to seperate parameters is not logical.

Link to comment
Share on other sites

Hey justsomeguy / Synook,I am discussing on this issue with Lulzim :) By the way, I am unable to change the properties of "upload" folder and hence I have opened a ticket. Expecting their [service provider's] response shortly :)TOPIC CLOSED!!!... Thanks a lot!!!...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...