PDA

View Full Version : php-upload-programm



Exos Sho
June 19th, 2006, 07:00 PM
yesterday i started writing a little program, which uploads files (till now pics prefered) to the folder, which contains the PHP-file.
on the first page, u select the file u want to upload, and may enter a Title( in order to enable a gallery-database with my-sql);
second page shows u wether the data has been sent to the database(THIS IS THE MAIN PROBLEM because it wont save anything to the database), info about the file, where its gonna be saved, or if a file(pic) alrady exists with the same name, in this case the script should view the "new-version"(which cant be uploaded) and the "old-version"(which alrady exists"but problem is, it wont show the already existing file
here are the two scripts, and css(in order u need to see more)
FIRST FILE,


<html>
<head>
<link rel="stylesheet" type="text/css" href="style_1.css">
</head>
<body>
<div style="background-color:#efefef;width:480px;border:5px solid #00c000;padding:1px;margin:0px auto;text-align:center;">
<form enctype="multipart/form-data" action="showit.php" method="post">
data: <input name="upfile" type="file" size="25">
<p>titel: <input name="titel" size="25">
<p><input type="submit" value="send" name="sent">
</form>
</div>
</body>
</html>

SECOND FILE


<html>
<head>
<link rel="stylesheet" type="text/css" href="style_1.css">
</head>
<body>
<div style="background-color:#efefef;width:480px;margin:0px auto;border:5px solid #00c000;padding:1px;text-align:center;">
<?php
if(isset($sent))
{
mysql_connect(localhost,root);
mysql_select_db("daten");
$sqlab = "insert upload (name, typ, titel) values ($upfile_name, $upfile_type, $titel)";
mysql_query($sqlab);
$num = mysql_affected_rows();
}
if ($num>0)
{
echo "<g>one data has been added<p></g>";
}
else if($num<=0)
{
echo "<r>an Error appeared, ";
echo "no data has been added<p></r>";
}
echo "<h3><ul><k>control:</k></ul></h3><p>";
echo "original-name: <zitat>$upfile_name</zitat><br>";
echo "size: <zitat>$upfile_size Byte</zitat><br>";
echo "type: <zitat>$upfile_type</zitat><br>";

$dname = explode(".",$upfile_name);
$dnamesize = count($dname);
$upfile_ext = $dname[$dnamesize-1];
echo "data-extension: <zitat>" . $upfile_ext . "</zitat><br>";
list($headw, $headh) = getimagesize($upfile);
echo "original-properties: <zitat>" . $headw . "x" . $headh . "</zitat><br>";
$verz = getcwd();
$exi = $verz ."/". $upfile_name;
if($upfile_size>0 && !file_exists($exi))
{
echo "new home of " . $upfile_name . " : <zitat>" . $verz. "</zitat><br>";
copy($upfile,$upfile_name);
echo "data has been copied in: <zitat>$upfile_name</zitat><br>";
if($headw>470)
{
echo "<img src= $upfile_name width= '470'>";
}
else
{
echo "<img src= $upfile_name>";
}
}
else if(file_exists($exi))
{
echo "<r>a file with the name: <zitat>" . $upfile_name . "</zitat> does already exist!</r><br>";
echo "<table>";
echo "<tr><td><k>new-" . $upfile_name . "</k></td><td><k>old-" . $upfile_name . "</k></td></tr>";
echo "<tr><td><img src= $upfile_name width= '200'></td><td><img src= $exi width= '200></td></tr>";
echo "</table>";
}
else
{
echo "copy-error: data does not exist";
echo " or false data-type";
}
?>
</div>
</body>
</html>
and the CSS, which isnt really necessary


/*----standarts-*/
div {background-color:#efefef;width:480px;margin:0px auto;border:5px solid #00c000;padding:1px;text-align:center;}
body{font-family:Verdana; font-size:10pt; color:#636363; background-color:#c3c3c3}
* {font-size:10pt;color:#636363;}
/*-*/
/*link*/
a:link {color:636363}
a:visited {color:606060}
a:hover {color:606060; background-color:#a3a3a3}
/*-*/
/*tabel*/
table {border:solid 3px #ffffff;background-color:#efefef;margin:auto}
td {font-family:Verdana; font-size:10pt; color:#636363; background-color:#c3c3c3}
/*_____________________*/


/*----class-selector-*/
.top {background-color:#CCCCCC; color:#333333}
.ul {text-decoration:underline}
.ol {text-decoration:line-through}
.fett {font-weight:bold}
.italic {font-style:italic}
/*-*/
/*color*/
.red {color:#FF0000}
.blue {color:#0000FF}
.green {color:#00FF00}
.yellow {color:#FFFF00}
.key {color:#000000}
.white {color:#FFFFFF}
/*_____________________*/


/*----typ-selector-*/
/*text-typen*/
zitat {text-shadow:#333333; color:#0066FF}
/*font-color*/
r {color:#FF0000}/*red*/
b {color:#0000FF}/*blue*/
g {color:#00FF00}/*green*/
y {color:#FFFF00}/*yellow*/
k {color:#000000}/*key*/
w {color:#FFFFFF}/*white*/
/*-*/
/*background-color*/
runder {background-color:#FF0000}/*red*/
bunder {background-color:#0000FF}/*blue*/
gunder {background-color:#00FF00}/*green*/
yunder {background-color:#FFFF00}/*yellow*/
kunder {background-color:#000000}/*key*/
wunder {background-color:#FFFFFF}/*white*/
/*-*/
/*font-decoration*/
top {background-color:#CCCCCC; color:#333333}
ul {text-decoration:underline}
ol {text-decoration:line-through}
fett {font-weight:bold}
italic {font-style:italic}
/*-*/

maybe i have to say that i am a very noob in php, and php/mysql, so please be patient:ub:
thank u

Neolumi
June 20th, 2006, 05:18 AM
hey, theres a problem with your mysql insert query, it should be:

$sqlab = "insert into upload (name, typ, titel) values ($upfile_name, $upfile_type, $titel)";

Neolumi
June 20th, 2006, 05:31 AM
oh and about outputting your already existing image, you have a space in there and should really use quotation marks or whatever you call em:

echo "<tr><td><img src= $upfile_name width= '200'></td><td><img src= $exi width= '200></td></tr>";
with echo or print you can change the ' ' or " " (sorry too early, brain no worky). e.g:

echo '<tr><td><img src="'.$upfile_name.'" width="200"></td><td><img src="'.$exi.'" width="200"></td></tr>';

Exos Sho
June 20th, 2006, 05:41 AM
thank u very much ^^,
but unfortunately the mysql part still wont work

Neolumi
June 20th, 2006, 05:56 AM
oh yeah, I dont see where your getting the variable $upfile_type from, depending on how you set the row your query may not work without a value (could be wrong).

A way to get your filetype is through mime types, or exploding the filename string like you have done, if you don't already have it go to php.net and download the manual.
An example of the mime type method would be:

$upfile_type = mime_content_type($filenamewithpath);
which would return "image/gif" for a gif file.

Also regarding passing variables you should use the relevant $_POST['varname'] or $_GET['VARNAME'], as your posting variables from your first file form. Like checking the form has been sent in the second file: "if(isset($sent))" would be "if(isset($_POST['sent']))".

Exos Sho
June 20th, 2006, 09:39 AM
the $upfile_type-thing works perfectly,
and i've changed the isset-thing, but still dont work, unfortunately
but thank u anyway for your help ^^

Exos Sho
June 21st, 2006, 03:58 PM
i got it working perfectly now(well maybe not really perfectly)
i solved the mysql thing


...
if(isset($_POST['send']))
{
@mysql_connect("localhost", "root")
or die("Could not connect to database");
@mysql_select_db("daten")
or die("Could not connect to database");

$query = ("INSERT INTO upload (name,typ,titel) VALUES ('$upfile_name','$upfile_type','$titel')");
$result = mysql_query($query) or die('Query failed: ' . mysql_error());


$num = mysql_affected_rows();
}
if ($num>0)
{
echo "<g>one data has been added<p></g>";
}
else if($num<=0)
{
echo "<r>an Error appeared, ";
echo "no data has been added to mysql<p></r>";
}
...

and the old-file, new-file thing, i now save also the new-pic but every time with an(undynamic)-name(which always is : redrasterblau) and show this;


else if(file_exists($exi))
{
copy ($upfile, "redrasterblue.png");
echo "<r>a file with the name: <zitat>" . $upfile_name . "</zitat> does already exist!</r><br>";
echo "<table>";
echo "<tr><td><k>new-" . $upfile_name . "</k></td><td><k>old-" . $upfile_name . "</k></td></tr>";
echo '<tr><td><img src="redrasterblue.png" width="200"></td><td><img src="'.$upfile_name.'" width="200"></td></tr>';
echo "</table><br>";
}