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!
I'm wondering if someone could help me with an document upload problem?
I had a script working fine except that it didn't do exactly what I wanted it to - so I tweaked it. In fact I tweaked it so much that it no longer works!
Anyway - here it is:
The script takes the information sent by a form and enters the text fields info into a db along with the document name then it moves the document itself to the uploads folder.
Yeah the original script appended the upload directory onto the file name but I had to change that as the only way it worked was for the upload directory path to be /home/username/html/uploads which was no good if appended to the file name but if I tried just having /uploads/ as the specified directory then it didn't work.
(bearing in mind that I'm not very good at PHP) I tried to do this:
/uploads would be the directory "uploads" in the root. I doubt the directory is there. Note how "/home/username/html/uploads" is a full system path.
$nl_name is being set to the array associated with the information for your uploaded file. You need to use the basename() of the 'name' element from that array.
$uploaddir = '/home/username/public_html/uploads/';
$uploadfile = $uploaddir . basename($_FILES['nl_name']['name']);
include("/home/username/public_html/includes/dbsetup.php");
// Making variables
$nl_id = $_POST['nl_id'];
$nl_title = $_POST['nl_title'];
$nl_date = $_POST['nl_date'];
$result=MYSQL_QUERY("INSERT INTO newsletter (nl_id,nl_title,nl_date,uploadfile)".
"VALUES ('NULL', '$nl_title', '$nl_date', '$uploadfile' )");
// save the info to the database
$results = mysql_query( $query );
if (move_uploaded_file($_FILES['nl_name']['tmp_name'], $uploadfile)) {
echo "<h2>Sucessful Upload</h2><p>File is valid, and was successfully uploaded.</p>";
} else {
echo "<p>Problem Uploading</p>";
}
The problem is that it inserts the full path (i.e. /home/username/public_html/uploads/filename.doc) into the DB and that's no use to me as I need a more useable path for the file.
So what do I need to change to get it to upload the file but without appending the full path (pref just the filename)?