[solved] A different way for "uploading?"
Moderator: General Moderators
[solved] A different way for "uploading?"
Sometimes I get stuff and sometimes I don't.
The concept of PHP file and image uploads is something that I don't fully understand.
Is there a way I can have a file browse field in a form and when the form is submitted, the script will copy the file to a directory on my server and store a path in a database? Then when I want to show the picture on a page, just use the link in the db to display the picture?
The concept of PHP file and image uploads is something that I don't fully understand.
Is there a way I can have a file browse field in a form and when the form is submitted, the script will copy the file to a directory on my server and store a path in a database? Then when I want to show the picture on a page, just use the link in the db to display the picture?
Last edited by dallasx on Thu Nov 10, 2005 3:21 pm, edited 1 time in total.
Jcart | Please use
Jcart | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
yes you can. I worked on this puppy a few weeks ago... you should be able to change it to your needs. I was doing this for pdf files.Code: Select all
<?php
if ($_POST[user])
{
$db = mysql_connect('localhost', 'your_dbname', 'your_table')
or die("Cant connect to DB");
mysql_select_db('metroins_reports') or die("cant select the DB");
$result = mysql_query("SELECT * FROM reports",$db) or die ("cant query DB for all");
$result2 = mysql_query("SELECT * FROM reports WHERE user='$_POST[user]'",$db) or die ("cant query DB for all");
}
else
{
echo "
<form action=reports.php method=post>
<input type=hidden name=getreport value=yes>
What is your User ID: <input type=text name=user><br>
<input type=submit value='Get My Report'>
</form>
";
}
if ($_POST[user]==superuser)
{
echo "
<a href=reports.php>Click here to logout</a>
<form enctype='multipart/form-data' action=reports.php method=post>
<input type=hidden name=user value=jmauldin>
<input type='hidden' name='MAX_FILE_SIZE' value='30000' />
<table><tr><td>
User ID:</td><td><input type=text name=adduser></td></tr><tr><td>
Report:</td><td><input type=file name=uploadedfile></td></tr><tr><td>
<input type=submit value='Add Report'></td></tr></table>
</form><table border=1 width=250>";
while ($row = mysql_fetch_array($result))
{
echo "<tr><td>$row[user]</td><td>$row[report]</td>";
}
echo "</table>";
}
else if ($_POST[user])
{
$result2 = mysql_fetch_array($result2);
if ($result2[user]!="")
{
echo "Click the link below for your report. Thank you!<br>";
echo "<a href=reports/$result2[report]>Report for $result2[user]</a>";
}else{
echo "That report does not exist. Please contact Jim Mauldin for details.";
}
}
if ($_POST[adduser])
{
$user = $_POST[adduser];
$report = $_FILES['uploadedfile']['name'];
$target_path = "reports/";
$target_path = $target_path . basename($report);
mysql_query("INSERT INTO reports VALUES ('$user','$report')",$db) or die ("cant add into db check that user is unique");
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path))
{
$link = "http://yoursite/reports/".basename($_FILES['uploadedfile']['name']);
echo "File is valid, and was successfully uploaded.<br>";
echo "<a href=$link>Click here to view it</a>";
}
}
?>Jcart | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm
Let's say if I wanted to drop the upload stuff into my main form that adds an item to a database... do I have to have the ENCTYPE?
If I do, what will that do to the current form, if anything?
Code: Select all
<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>-
nickvd
- DevNet Resident
- Posts: 1027
- Joined: Thu Mar 10, 2005 5:27 pm
- Location: Southern Ontario
- Contact:
If you want a form to be able to upload a file, you MUST include the enctype="multipart/form-data" attribute, or else the form will submit as normal WITHOUT uploading the file.dallasx wrote:Let's say if I wanted to drop the upload stuff into my main form that adds an item to a database... do I have to have the ENCTYPE?If I do, what will that do to the current form, if anything?Code: Select all
<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>
Yeah. right after I posted that, I went ahead and tried it. It worked. I have never seen that before except when dreamweaver prompts it and stuff when yer typing the form tag. I didn't know if it was for files only and tweak the other form data. Everything checks out!nickvd wrote: If you want a form to be able to upload a file, you MUST include the enctype="multipart/form-data" attribute, or else the form will submit as normal WITHOUT uploading the file.
Thanks!
Code: Select all
<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>read the bit about $_SERVER['PHP_SELF']