Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Syntax Question

Status
Not open for further replies.

kjspear

Programmer
Feb 13, 2002
173
US
How do I attach a field to the link below?

$uploaddir = 'c:/mysql/data/mylogin/upimages/';

I would like to do something like this;

$uploadeddir='c:/mysql/data/mylogin/upimages/$FILENAME;

What is the proper syntax as listed above?

Any help would be appreciated.

Thank you
KJ
 
What exactly are you trying to do here? Are you uploading a file?

Nate

mainframe.gif

 
Yes I'm trying tp upload a file. Here's the HTML code;

html>
<head>
<title>Upload Your Photos</title>
</head>
<body>
<h2>Upload Your Photos</h2>

<form enctype=&quot;multipart/form-data&quot; method=&quot;post&quot; action=&quot;mypicts1.php&quot;>
<input type=&quot;file&quot; name=&quot;userfile&quot; size=&quot;30&quot;>
<br><br>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Send this file&quot;>

</form>
</body>
</html>
------------------------Here's the php code from another file----------
<?php
$conn=mysql_connect(&quot;localhost&quot;,&quot;chelsea&quot;,&quot;&quot;)
or die(&quot;Could not connect &quot; . mysql_error());
mysql_select_db(&quot;mylogin&quot;,$conn) or die(&quot;Could not select database&quot;);

$uploaddir = 'c:/mysql/data/mylogin/upimages/';

print &quot;<pre>&quot;;

if (!copy($_FILES['userfile']['tmp_name'], $uploaddir . $_FILES['userfile']['name'])) {
print &quot;File is valid, and was successfully uploaded. Here's some more debugging info:\n&quot;;
print_r($_FILES);

$sql=&quot;INSERT INTO myimagexx (imagefilepath) VALUES ('image015.jpg')&quot;;
$result=mysql_query($sql, $conn) or die(mysql_error());
echo $result;
//mysql_select_db(&quot;mylogin&quot;,$conn) or die(&quot;Could not select database&quot;);

} else {
print &quot;Possible file upload attack! Here's some debugging info:\n&quot;;
print_r($_FILES);

//$conn=mysql_connect(&quot;localhost&quot;,&quot;mkt2000&quot;,&quot;chelsea7&quot;)
//or die(&quot;Could not connect &quot; . mysql_error());
$sql=&quot;INSERT INTO myimagexx (imagefilepath) VALUES ('$uploaddir')&quot;;
$result=mysql_query($sql, $conn) or die(mysql_error());
print $result;
mysql_select_db(&quot;mylogin&quot;,$conn) or die(&quot;Could not select database&quot;);


}

?>


What I'm trying to do is upon the selection of the file in the HTML code, Take the name of the file and insert it into the MYSQL database. This way when the record is queried, the filename of the image can be retrieved and displayed in an HTML or PHP file.
I can do the latter part. All I need to know how to do is talking the user's selection and inserting it into the database.

Thank you,
KJ

 
I'm not sure what you're trying to do, but if you're inserting the name of the uploaded file, your code is nearly there. In your INSERT query, use $_FILES['userfile]['name'] instead of the hard-coded value. I'm not sure what you mean by &quot;upon the selection of the file in the HTML code&quot;, as your posted code doesn't seem to have any functionality for selecting anything.


A couple of other comments. When moving an uploaded file from the temporary store to the file's permanant location in the filesystem, don't use copy(), but rather move_uploaded_file(). The latter function makes additional checks that the file you're moving was actually uploaded, so it can stave off one type of possible error.

I see that you are echoing the result of a mysql_query(). That's not going to get you anywhere, as that result can be either a boolean or a database resource handle, none of which are printable.

I also see that you are not checking for errors with your database connection code. Adding at least &quot;or die(mysql_error())&quot; to your mysql_connect(), mysql_select_db(), and mysql_query() lines can give you debugging information your code won't provide you.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Hi,

Let me point something else out to you, you are showing a path to your c: drive yet you want to be able to path to that to show on the web. If you use the absolute path then that image will never show except on your computer, it will look for that file on the users computer.

You want to have an upload dir for where the image is stored but you will need to convert that to a server path and then add to filename onto it, then insert that into your db, so that it is pathed correctly for the user to see.

Hope this helps!

PS - might be a good idea to NOT to tell us your username and password ;-)

Nate

mainframe.gif

 
O.K. Cool!

I almost got it to work. Now I just have a minor problem. I'm sure its a simple one. How do I take the value in the array below so that I can insert it into the database?

$_FILES=array('[name]','[type]','[tmp_name]','[error]','[size]');
print $_FILES[0];
$image=$uploaddir.$_FILES[0];

$sql=&quot;INSERT INTO myimagexx (imagefilepath) VALUES ('$image')&quot;;

If I use the command print $FILES[0], it will display the array I want which is '[name]'. That's good. But that's exactly what it is printing out. I need to print the value of this array [name]. I tried using ['.name.'], but that does not work.

Can anyone tell me what the appropiate syntax is? The program will work once I have the correct syntax.

Thanks,
KJ.
 
Why are you doing it the hard way?

This is what you want to do:

if (is_uploaded_file($_FILES[&quot;userfile&quot;][&quot;tmp_name&quot;]))
{
$path = &quot;c:/path/to/folder/&quot;;

if (($_FILES['userfile']['type']==&quot;image/gif&quot;) || ($_FILES['userfile']['type']==&quot;image/pjpeg&quot;) || ($_FILES['userfile']['type']==&quot;image/jpeg&quot;))
{
$res = move_uploaded_file($_FILES['userfile']['tmp_name'], $path . $_FILES['userfile']['name']);
if ($res)
{
$connect = mysql_connect(&quot;$host&quot;,&quot;$username&quot;,&quot;$password&quot;);

mysql_select_db(&quot;$db_name&quot;,$connect);

$sql = &quot;INSERT INTO `$table_name` ( `imagepath`) VALUES ('&quot; . $_FILES[&quot;userfile&quot;][&quot;name&quot;] . &quot;')&quot;;

$query = mysql_query ($sql);

mysql_close($connect);
}
else
{
echo &quot;temp file not moved correctly&quot;;
}
else
{
echo &quot;image is not .gif or .jpg, re-upload&quot;;
}
else
{
echo &quot;field was not filled in, please upload your file&quot;;
}

Substitute the red bold parts with the name of your file field in your form, and substitute the blue bold part with the correct name of your db row.

Hope this helps!

Nate

mainframe.gif

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top