30. december 2007 - 15:08Der er
13 kommentarer og 1 løsning
Tilføje tekstområde til billedupload
Hej.
Jeg har et uploadsystem til billeder. Jeg vil dog gerne have så jeg kan tilføje noget tekst der kan blive vist sammen med billedet. Er det noget i kan hjælpe med?
#upload-ok.php# <?php // if something was posted, start the process... if(isset($_POST['upload'])) {
// define the posted file into variables $name = $_FILES['picture']['name']; $tmp_name = $_FILES['picture']['tmp_name']; $type = $_FILES['picture']['type']; $size = $_FILES['picture']['size'];
// get the width & height of the file (we don't need the other stuff) list($width, $height, $typeb, $attr) = getimagesize($tmp_name);
// if width is over 600 px or height is over 500 px, kill it if($width>'600' || $height>'500') { echo $name . " dimensions exceed the 600x500 pixel limit"; ?> <p><a href="form.html">Click here</a> to try again. <? ; die(); }
// if the file size is larger than 350 KB, kill it if($size>'350000') { echo $name . " is over 350KB. Please make it smaller."; ?> <p><a href="form.html">Click here</a> to try again. <? ; die(); }
// if your server has magic quotes turned off, add slashes manually if(!get_magic_quotes_gpc()) { $name = addslashes($name); }
// if the mime type is anything other than what we specify below, kill it if(!( $type=='image/jpg' || $type=='image/jpeg' || $type=='image/pjpeg' || $type=='image/x-png' || $type=='image/png' || $type=='image/gif' )) { echo $type . "is not an acceptable format."; ?> <p><a href="form.html">Click here</a> to try again. <? ; die(); }
// open up the file and extract the data/content from it $extract = fopen($tmp_name, 'r'); $content = fread($extract, $size); $content = addslashes($content); fclose($extract);
// connect to the database include "../config.php";
// the query that will add this to the database $addfile = "INSERT INTO files (name, size, type, content ) ". "VALUES ('$name', '$size', '$type', '$content')";
mysql_query($addfile) or die(mysql_error());
// get the last inserted ID if we're going to display this image next $inserted_fid = mysql_insert_id(); mysql_close();
// display the image ?> <div align="center"> <strong><? echo $name; ?><br> </strong><img name="<? echo $name; ?>" src="getpicture.php?fid=<? echo $inserted_fid; ?>" alt="Unable to view image #<? echo $inserted_fid; ?>"> <br> <a href="form.html">upload more images</a> </div> <? // we still have to close the original IF statement. If there was nothing posted, kill the page. }else{die("No uploaded file present"); } ?>
Den moderne arbejdsplads er i stigende grad afhængig af mødelokaler til at fremme samarbejde, men dette skift medfører også stigende sikkerhedsudfordringer.
#upload-ok.php# <?php // if something was posted, start the process... if (isset($_POST['upload'])) { // define the posted file into variables $name = $_FILES['picture']['name']; $tmp_name = $_FILES['picture']['tmp_name']; $type = $_FILES['picture']['type']; $size = $_FILES['picture']['size'];
// get the width & height of the file (we don't need the other stuff) list($width, $height, $typeb, $attr) = getimagesize($tmp_name);
// if width is over 600 px or height is over 500 px, kill it if ($width > 600 || $height > 500) { echo $name . " dimensions exceed the 600x500 pixel limit"; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// if the file size is larger than 350 KB, kill it if ($size > 350000) { echo $name . " is over 350KB. Please make it smaller."; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// if your server has magic quotes turned off, add slashes manually if (!get_magic_quotes_gpc()) { $name = addslashes($name); }
// if the mime type is anything other than what we specify below, kill it if ($type != 'image/jpg' && $type != 'image/jpeg' && $type != 'image/pjpeg' && $type != 'image/x-png' && $type != 'image/png' && $type != 'image/gif') { echo $type . "is not an acceptable format."; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// open up the file and extract the data/content from it $extract = fopen($tmp_name, 'r'); $content = fread($extract, $size); $content = addslashes($content); fclose($extract);
// Get the desctiption. $description = $_POST['description']; $description = mysql_real_escape_string($description);
// connect to the database include "../config.php";
// the query that will add this to the database $addfile = "INSERT INTO files (name, description, size, type, content) VALUES ('$name', '$description', '$size', '$type', '$content')"; mysql_query($addfile) or die(mysql_error());
// get the last inserted ID if we're going to display this image next $inserted_fid = mysql_insert_id(); mysql_close();
// display the image ?> <div align="center"> <strong><? echo $name; ?><br> </strong><img name="<? echo $name; ?>" src="getpicture.php?fid=<? echo $inserted_fid; ?>" alt="Unable to view image #<? echo $inserted_fid; ?>"> <br> <a href="form.html">upload more images</a> </div> <? // we still have to close the original IF statement. If there was nothing posted, kill the page. } else { die("No uploaded file present"); } ?>
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\xampp\htdocs\recaroshop.dk\test\picupload\upload-ok.php on line 58
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in C:\xampp\htdocs\recaroshop.dk\test\picupload\upload-ok.php on line 58
Ved ikke om mit spørgsmål er forstået forkert eller om jeg bare opfatter det forkert. skal der ikke bruges et textarea eller noget? Så der kunne være mulighed for at lave linjeskift og sådan.. altså skrive lidt om billedet...
Nu har jeg bare lavet det som et alm. input=text felt, men der er intet i vejen for at du laver det som et textarea i stedet. Det er den samme grund kode der skal bruges. Hvis du ønkser at bevare linjeskift, skal der så også lige et kald til nl2br() med i koden.
Det lyder straks lidt mere underligt at den ikke acceptere mysql_real_escape_string(). Men der er måsk nok at du flytter din:
include "../config.php";
op, til lige før linjen hvor mysql_real_escape_string() bruges.
Nu har jeg flyttet config.php længere op, og det virker nu ser det ud til.. Jeg får bare en fejl nu fra sidst i scriptet "Unable to view image #3"... Kan det være noget i min getpicture.php der ikke er sat op til tekst???
#getpicture.php# <? if(isset($_GET['fid'])) { // connect to the database include "../config.php";
$fid = $_GET['fid']; $query = "SELECT * FROM files WHERE fid = '$fid'"; $result = mysql_query($query) or die(mysql_error());
// define results into variables $name=mysql_result($result,0,"name"); $size=mysql_result($result,0,"size"); $type=mysql_result($result,0,"type"); $content=mysql_result($result,0,"content");
// give our picture the proper headers...otherwise our page will be confused header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); header("Content-type: $type"); echo $content;
mysql_close(); }else{ die("No file ID given..."); }
Jeg går ud fra at den kode har virket tidligere? ... og da den ikke prøver at gøre noget med description-feltet burde den heller ikke fejle nu.
Det eneste jeg kan forestille mig som eventuelt skulle kunne drille, er hvis du har indsat description-feltet i tabellen efter content-feltet. Jeg er ikke helt klar over årsagen, men det plejer at virke bedre hvis blobfelter, som dit content, ligger sist i SQL'en.
Det kan man ikke gøre på den måde. Billedet og teksten er stadig forsellige to ting.
Den kode du har henter og viser/downloader selve billedet. For at vise teksten samtidig, så skal billedet indlejres i en større side hvor du så kan vise teksten ved siden af.
Nu har jeg tilføjet lidt til min upload-ok.php, så teksten bliver vist.. Hvordan er det så jeg bruger, så jeg kan beholde linjeskift?
<?php // if something was posted, start the process... if (isset($_POST['upload'])) { // define the posted file into variables $name = $_FILES['picture']['name']; $tmp_name = $_FILES['picture']['tmp_name']; $type = $_FILES['picture']['type']; $size = $_FILES['picture']['size'];
// get the width & height of the file (we don't need the other stuff) list($width, $height, $typeb, $attr) = getimagesize($tmp_name);
// if width is over 600 px or height is over 500 px, kill it if ($width > 6000 || $height > 5000) { echo $name . " dimensions exceed the 6000x5000 pixel limit"; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// if the file size is larger than 350 KB, kill it if ($size > 3500000) { echo $name . " is over 3500KB. Please make it smaller."; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// if your server has magic quotes turned off, add slashes manually if (!get_magic_quotes_gpc()) { $name = addslashes($name); }
// if the mime type is anything other than what we specify below, kill it if ($type != 'image/jpg' && $type != 'image/jpeg' && $type != 'image/pjpeg' && $type != 'image/x-png' && $type != 'image/png' && $type != 'image/gif') { echo $type . "is not an acceptable format."; ?> <p><a href="form.html">Click here</a> to try again. <? die(); }
// open up the file and extract the data/content from it $extract = fopen($tmp_name, 'r'); $content = fread($extract, $size); $content = addslashes($content); fclose($extract);
// connect to the database include "config.php";
// Get the description. $description = $_POST['description']; $description = mysql_real_escape_string($description);
// the query that will add this to the database $addfile = "INSERT INTO files (name, description, size, type, content) VALUES ('$name', '$description', '$size', '$type', '$content')"; mysql_query($addfile) or die(mysql_error());
// get the last inserted ID if we're going to display this image next $inserted_fid = mysql_insert_id(); mysql_close();
</tr> "; } ?> <a href="add.php">upload more images</a> </div> <? // we still have to close the original IF statement. If there was nothing posted, kill the page. } else { die("No uploaded file present"); } ?>
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.