[solved] A different way for "uploading?"

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!

Moderator: General Moderators

Post Reply
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

[solved] A different way for "uploading?"

Post 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?
Last edited by dallasx on Thu Nov 10, 2005 3:21 pm, edited 1 time in total.
cspatter
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2005 1:28 pm

Post 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]
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post by dallasx »

Thank you! I'll check it out and let you know how it goes. I appreciate the quick reply.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post by dallasx »

Works beautifully! Thanks!

Now i'll dissect the code to get the logic.
cspatter
Forum Newbie
Posts: 7
Joined: Fri Jul 08, 2005 1:28 pm

Post by cspatter »

Cool!! I like when my code is used elsewhere... you'll have to send me a link.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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::
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

assuming you're death would ruin it for us..heh.just playing
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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?
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

dallasx wrote:Har har! Heheheh
HAR DE HAR DE HAR HAR!
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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.
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post 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!
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post 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']
User avatar
dallasx
Forum Contributor
Posts: 106
Joined: Thu Oct 20, 2005 4:55 pm
Location: California

Post by dallasx »

Dagummit

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