0

Hello everyone,
I would really appreciate if anyone could please help me out here. I am trying to read the user's input in one page and then trying to display it in another page (want to let my users upload a file from their machine and save them in my database).
here's what i have so far....

in File_1.php I am collecting the file's path/location and description and saving then in two different variables:

<!-- letting the browser know that we are sending files from the client's machine to the server -->
<form enctype="multipart/form-data" name="frmUploadFile" action="File_2.php" method="post">

<br>Please select a file and give a description of it.<br>

<!-- insert the file location  -->
<tr>
<td>
<p>File Location: 
</td>

<td>
<input type="file" name="fileUpload" size="50">
</td>
</tr>

<!-- insert the file description  -->  
<tr>
<td>
<p>File Description: 
</td>

<td>
<input type="text" name="fileDesc" size="50">
</td>
</tr>

<tr>
<td width="67%">
<input type="submit" value="Submit" name="submit">
</td>
</tr>

and in File_2.php, I am checking if the user provided both fields:

// declared five global variables
global $fileName;  
global $fileSize;
global $fileDesc;  
global $fileType;  
global $fileUpload;

// Making sure both the file's location and description have been entered
if($fileUpload == "none")
	die("You haven't selected any files."); 
elseif(empty($fileDesc))
	die("You didn't put the file description.");

Now my problem is even though I am providing both fields in File_1.php, I am ALWAYS getting the message "You didn't put the file description." Can anyone please tell me what I am doing wrong?

Thank you in advance!!!

PS: i omitted few lines here that handled the font style/size etc. guessing that's not related to my question.

2
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by Javvy
0

try to echo $fileDesc or echo $_POST on File_2.php and see if you get any value.

0

May be i did some thing wrong, because i did

echo $fileUpload;
echo $fileDesc;

in my File_2.php file, and it's returning nothing :(
any ideas plz?

0

have you tried..

echo $_POST['fileDesc'];

I think you did not set any value to the global variables.

Edited by Javvy: n/a

0

Thank you for your reply!!!
sorry, that time i didn't try "echo $_POST" and after doing so it's showing me the file description ("The apple") but if i do....

echo $HTTP_GET_VARS['fileUpload'];  
echo $HTTP_POST_VARS['fileUpload'];

looks like that field is still empty. And I am attaching a screen shot of what I am uploading so you can see what i am doing.

and sorry for all the silly questions, i am totally new with PHP
thanks again!

Attachments upload.jpg 17.95 KB
0

I think you can just replace global variables with the following. However, I'm not sure whether you have correctly obtained the rest of the values. From your codes above, I can only see that you have $_POST and $_POST.

$fileName = $_POST['fileName'];  
$fileSize = $_POST['fileSize'];  
$fileDesc = $_POST['fileDesc'];  
$fileType = $_POST['fileType'];  
$fileUpload = $_POST['fileUpload'];
0

The file description is showing up, but nothing else.

This is what I have right now in my File_2.php....

$fileName = $_POST['fileName'];
$fileSize = $_POST['fileSize'];
$fileDescription = $_POST['fileDescription'];
$fileType = $_POST['fileType'];
$fileUpload = $_POST['fileUpload'];

echo $_POST['fileDescription<br>'];
//echo $fileName;
//echo $_POST['fileType'];
//echo $_POST['fileSize'];
echo $_POST['fileUpload']; 	//HTTP_GET_VARS['fileUpload'];  //$HTTP_POST_VARS['fileUpload'];


// Making sure both the file's location and description have been entered
if(empty($fileDescription))
	die("You didn't put the file description.<br>"); 
elseif($fileUpload == "none")
	die("You haven't selected any files.<br>"); 

	
// uploaded file from the local directory on the web server, and read its contents 
$fileHandle = fopen($fileUpload, "r");
$fileContent = fread($fileHandle, $fileSize);
$fileContent = addslashes($fileContent);

// Database connection set up
$dbServer = "*****.***.***.***";
$username = "*****"; 
$password = "****";  
$database = "*****";  

// connect the database using PHP's built-in MySQL functions
$mysql_access = mysql_connect($dbServer, $username, $password);
@mysql_select_db($database) or die( "Unable to connect to the database<br>"); 

//inserting values to the table
mysql_query("INSERT INTO ImageBLOB (imageID, imageDescription, imageData, imageType) VALUES (0, '$fileDescription', '$fileContent', '$fileType')");

mysql_query($dbQuery) or die("Couldn't add the file to database<br>");

mysql_close($mysql_access);

seems like after trying to upload a file with description, I can connect to my database. However, when I am trying to INSERT those info to my ImageBLOB table, I get the "Couldn't add the file to database" error message. Yet, if i do the "SELECT * FROM ImageBLOB" I see that this command added one entry to the table without "imageData" & "imageType"

any suggestions please? Thank you!!

Attachments upload.jpg 18.05 KB
0

Try this:

$fileDescription = $_POST['fileDescription'];
if(empty($fileDescription)) {
die("You didn't put the file description.<br>");
}

$name_of_uploaded_file = basename($_FILES['fileUpload']['name']);
$type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1);

$size_of_uploaded_file = $_FILES["fileUpload"]["size"]/1024; //size in KBs

//insert into DB

note: there is no error checking.

Edited by Javvy: n/a

0

Thank you Javvy for all your helps! i finally got it :)

$fileName = $_FILES['fileUpload']['name'];
$fileSize = $_FILES['fileUpload']['size'];
$fileType = $_FILES['fileUpload']['type'];
$tempFile = $_FILES['fileUpload']['tmp_name'];
$fileDescription = $_POST['fileDescription'];
0

No problem. Glad it's working and you can carry on with your work :)

You may want to mark the thread as 'solved'.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.