Page 1 of 1

[solved] A different way for "uploading?"

Posted: Fri Oct 28, 2005 11:56 am
by dallasx
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?

Posted: Fri Oct 28, 2005 12:10 pm
by cspatter
Jcart | Please use

Code: Select all

and

Code: 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

and

Code: 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]

Posted: Fri Oct 28, 2005 12:50 pm
by dallasx
Thank you! I'll check it out and let you know how it goes. I appreciate the quick reply.

Posted: Fri Oct 28, 2005 1:17 pm
by dallasx
Works beautifully! Thanks!

Now i'll dissect the code to get the logic.

Posted: Fri Oct 28, 2005 1:18 pm
by cspatter
Cool!! I like when my code is used elsewhere... you'll have to send me a link.

Posted: Fri Oct 28, 2005 1:52 pm
by dallasx
cspatter wrote:Cool!! I like when my code is used elsewhere... you'll have to send me a link.
I got the logic and rewrote a script that does the same thing. Mine stores the link in the db, the file name (no real point really) and hopefully soon, the product id.

Posted: Fri Oct 28, 2005 1:54 pm
by Luke
YAY!!!
This sure was a happy ending. Everything worked out for everybody. Let's dance...

::dances::

::nobody joins in::

::cries::

::goes home and cries more::

::kills self::

::ruins happy little thread::

Posted: Fri Oct 28, 2005 1:56 pm
by Charles256
assuming you're death would ruin it for us..heh.just playing

Posted: Fri Oct 28, 2005 2:03 pm
by dallasx
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?

Code: Select all

<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>
If I do, what will that do to the current form, if anything?

Posted: Fri Oct 28, 2005 2:04 pm
by dallasx
One armed space goat wrote:YAY!!!
This sure was a happy ending. Everything worked out for everybody. Let's dance...

::dances::

::nobody joins in::

::cries::

::goes home and cries more::

::kills self::

::ruins happy little thread::
Har har! Heheheh

Posted: Fri Oct 28, 2005 2:12 pm
by Luke
dallasx wrote:Har har! Heheheh
HAR DE HAR DE HAR HAR!

Posted: Fri Oct 28, 2005 5:15 pm
by nickvd
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?

Code: Select all

<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>
If I do, what will that do to the current form, if anything?
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.

Posted: Sat Oct 29, 2005 11:06 am
by dallasx
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.
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!

Thanks!

Posted: Sat Oct 29, 2005 7:34 pm
by mickd

Code: Select all

<form enctype='multipart/form-data' action=".$_SERVER['PHP_SELF']." method=post>
viewtopic.php?t=39853

read the bit about $_SERVER['PHP_SELF']

Posted: Sun Oct 30, 2005 2:29 pm
by dallasx
Dagummit

So, in every form, just put in the actual file name for the action?