FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


image upload, and return url to image





ranzon
hi, i wonder if someone could help me with making an upload script for images, and print the url to image, i've give 150 FRIH$ for an finished script...
hexkid
ranzon wrote:
hi, i wonder if someone could help me with making an upload script for images, and print the url to image, i've give 150 FRIH$ for an finished script...


So you basically want something like this, right?

Code:
<!-- HTML form to upload image -->

Code:
<?php
/* script to save the image and */
/* display an URL that show the uploaded image (http://somewhere/display?id=42) */
?>
ranzon
hexkid wrote:
ranzon wrote:
hi, i wonder if someone could help me with making an upload script for images, and print the url to image, i've give 150 FRIH$ for an finished script...


So you basically want something like this, right?

Code:
<!-- HTML form to upload image -->

Code:
<?php
/* script to save the image and */
/* display an URL that show the uploaded image (http://somewhere/display?id=42) */
?>


yes thats right <3
if you could i had been very thankfull for make so the image beeing renamed do an unique filename too (A)
hexkid
As there is no (easy) way for the server to know what type the file is, I did the script to accept anything, and output it with the same type that was specified at the upload.

This script does all three functions you asked for:
If it's called without an "id" parameter it will show a form for upload;
if it's called as the result of clicking the "submit" button, it will deal with file uploads;
if it's called with an "id" parameter, it will display/download the file.


To make it work as I intended you need to create a table in a database you can access. The code for that is in a comment near the top of the script.
After that comment, you have three define's you need to redefine!


Of course, you may want to make the HTML prettier.

The code is also available at the frihost pastebin, but as 'pastebin' was misbehaving madly I thought it hadn't gone through so I also pasted it here (and mods are free to delete the code from here).
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

/* SQL
USE test;                                     -- copy database name to FILE_DATABASE
CREATE TABLE `upload` (                       -- copy table name to FILE_DATATABLE
  `file_id` int(11) NOT NULL auto_increment,
  `file_type` char(32) NOT NULL,
  PRIMARY KEY  (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/

define('FILE_DATABASE', 'test');
define('FILE_DATATABLE', 'upload');
define('UPLOAD_DIRECTORY', '/var/www/uploads/');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  if ($_FILES['f']['error']) {
    switch ($_FILES['f']['error']) {
      case 1: $msg = 'File too large for PHP'; break;
      case 2: $msg = 'File larger then HTML form limit'; break;
      case 3: $msg = 'Partial upload'; break;
      case 4: $msg = 'No file'; break;
      case 6: $msg = 'No temporary folder on server'; break;
      case 7: $msg = 'Error writing to disk on server'; break;
      default: $msg = 'Unspecified error'; break;
    }
    exit('Error: ' . $msg);
  }
  /* ********************************************
  // if you choose to trust $_FILES['f']['type'] you may want to filter on it
  if (!preg_match('/^image\/(?:p?jpe?g|png|gif)$/i', $_FILES['f']['type'])) {
    exit('Please upload only images (jpg, png, or gif)');
  }
  */
  $con = mysql_connect() or die(mysql_error());
  mysql_select_db(FILE_DATABASE) or die(mysql_error());
  $sql = 'insert ' . FILE_DATATABLE . ' (file_type) values (\'' . mysql_real_escape_string($_FILES['f']['type']) . '\')';
  mysql_query($sql) or die(mysql_error());
  $file_id = mysql_insert_id();
  mysql_close();
  if (!move_uploaded_file($_FILES['f']['tmp_name'], UPLOAD_DIRECTORY . $file_id)) {
    exit('Unable to deal with upload.');
  } else {
    $DownloadURI = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?id=' . $file_id;
    echo 'use <a href="' . $DownloadURI . '">' . $DownloadURI . '</a> to download/view the file.';
  }
} else {
  if (isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    if ($id < 1) {
      exit('No!');
    }
    $con = mysql_connect() or die(mysql_error());
    mysql_select_db(FILE_DATABASE) or die(mysql_error());
    $sql = 'select file_type from ' . FILE_DATATABLE . ' where file_id=' . $id;
    $res = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($res) > 0) {
      $file_type = mysql_result($res, 0);
      header('Content-Type: ' . $file_type);
      readfile(UPLOAD_DIRECTORY . $id);
    } else {
      echo 'Invalid id!';
    }
    mysql_free_result($res);
    mysql_close();
    exit(0);
  } else {
    $ThisURI = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    echo <<<HTML
<form action="$ThisURI" method="post" enctype="multipart/form-data">
Select file: <input type="file" name="f"><br>
<input type="submit" value="Upload">
</form>
HTML;
  }
}
?>
ranzon
hexkid wrote:

Code:


/* SQL
USE test;                                     -- copy database name to FILE_DATABASE
CREATE TABLE `upload` (                       -- copy table name to FILE_DATATABLE
  `file_id` int(11) NOT NULL auto_increment,
  `file_type` char(32) NOT NULL,
  PRIMARY KEY  (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/


this gives the error:
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARSET=utf8' at line 5
hexkid
ranzon wrote:
hexkid wrote:

Code:


/* SQL
USE test;                                     -- copy database name to FILE_DATABASE
CREATE TABLE `upload` (                       -- copy table name to FILE_DATATABLE
  `file_id` int(11) NOT NULL auto_increment,
  `file_type` char(32) NOT NULL,
  PRIMARY KEY  (`file_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
*/


this gives the error:
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARSET=utf8' at line 5


Try it without the engine and charset options
Code:

/* SQL
USE test;                                     -- copy database name to FILE_DATABASE
CREATE TABLE `upload` (                       -- copy table name to FILE_DATATABLE
  `file_id` int(11) NOT NULL auto_increment,
  `file_type` char(32) NOT NULL,
  PRIMARY KEY  (`file_id`)
);
*/
Related topics
Rad Upload Plus v1.22
Site Nav Suggestions...
PHP Image Upload (MySQL/Folder)
ImageShack's Tools
ImageShack's Tools
Upload Script
Porta trouble
Problem with Image upload mod from phpBBstyles
quite simple file upload
PHP image uploads with progress bar AND resizing? [Resolved]
SGM Image Upload&Download Script
Image hosting site i use
image upload to message post from hard drive
Pik.gy - FORUM / IMAGE UPLOAD / ALBUMS & PORTFOLIOS
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.