Hi,
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
<?php
//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
$file1=($_FILES['file1']['name']);
$file2=($_FILES['file2']['name']);
// 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."; }
?>
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.
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.
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 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.
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
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.