Having Problems In Uploading Files To Target Folder

I am new to PHP and learning it!
I have created a simple database on my localhost name ‘submitpaper’
Then I have created a table name ‘upload_file’ having two fields (file1, file2) both are (VARCHAR 255)
I am having problem in saving files to target folder ‘testupload’

Kindly Check and Review My PHP Script And HTML

PHP Script

//This is the directory where files will be saved
$target = "testupload/";
$target = $target . basename( $_FILES['file']['name']);

//This gets all the other information from the form

// Connects to your Database
mysql_connect("localhost", "root", "")
//Writes the information to the database
mysql_query("INSERT INTO `upload_file` VALUES ('$file1', '$file2')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))  {
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). "has been uploaded, and your information has been added to the directory";  }
else {
//Gives and error if its not  echo "Sorry, there was a problem uploading your file.";  }




<form action="upload_file.php" method="post"
File1:<input type="file" name="file1" id="file1"><br>
File2:<input type="file" name="file2" id="file2">
<input type="submit" name="submit" value="Submit">


When you say you have a problem, what is the problem? Do you get an error message? Have you checked the permissions on the upload file directory to make sure they allow files to be written?

Also you need to look at not using the mysql library for this. As you said you’re starting out with PHP, have a look at the mysqli or PDO libraries for database access, as mysql is being deprecated now. There is a link at the top of this board to articles on how to change. Better to get used to the newer ways than waste time with the old.

whenever i click the ‘submit’ button, the php code is displayed…

OK, how are you testing? Specifically, do you have a server (WAMP, MAMP, something else) running on your PC to interpret the PHP code? If you don’t, and you’re just opening the html code in the browser, that’s what you’ll get.

I am using WAMP SERVER on my local host!

Crank the error reporting level to maximum and turn on the display of errors (error display should be turned off for a live server)

The last comment has a normal line tagged to the end of it which has been commented out because of the comment anything on the same line as // is commented out, the echo line needs moving to a new line

Have to check to make sure the folder has read, write, post permissions? Something like 777? You cannot post to the folder if the permissions to do so is not right.

I would NEVER suggest changing permissions to 777 and would always suggest you NOT

There may be times when 7__ is required, but when would you ever want to allow anyone and eveyone to upload anything and then run that file?

I think that’s the only way PHP will write or post to a folder in a directory!

I can’t see why having bad permissions on the upload directory would cause the OPs system to just display his PHP code when he presses the submit button.

My guesses would include

The HTML file is being opened through the filesystem’s default browser instead of by an open browser requesting the file through the localhost server.
The PHP file is misspelled and is being opened as text instead of being processed by PHP

@danyal1990 if you open a browser and using the address bar go to
http://localhost or
what do you see?

It’s possible if the php script is not written to output an error message!

Oh, OK. I would have expected a blank page or an error message if OP hasn’t dealt with an error, rather than just displaying the php source. But I have little real experience of this.