Jump to content


Photo

File Uploader- Getting the location of the just uploaded file


  • Please log in to reply
5 replies to this topic

#1 HoTDaWg

HoTDaWg
  • Members
  • PipPipPip
  • Advanced Member
  • 275 posts
  • LocationToronto

Posted 13 March 2006 - 10:40 PM

Hello,

I am creating a photo gallery for the awsome people on my site (if any lol). What I am doing is once the image is uploaded, I want all the info added to a database. Everything is working great. The only problem is that I can not seem to get the location of the file once it is uploaded right. Take a look at the dumping data of my mySql database once I export it:

-- phpMyAdmin SQL Dump
-- version 2.6.4-pl2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Mar 13, 2006 at 05:22 PM
-- Server version: 4.1.14
-- PHP Version: 4.3.11
-- 
-- Database: `cortez_photoalbums`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `images`
-- 

CREATE TABLE `images` (
  `id` int(15) NOT NULL auto_increment,
  `imageurl` blob NOT NULL,
  `titled` varchar(255) NOT NULL default '',
  `contributedby` varchar(22) NOT NULL default '',
  `date` varchar(50) NOT NULL default '0000-00-00',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;

-- 
-- Dumping data for table `images`
-- 

INSERT INTO `images` VALUES (1, 0x2275706c6f6164732f22202e20245f46494c45535b2266696c65225d5b226e616d65225d, '4', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (2, 0x2275706c6f6164732f225b2266696c65225d5b226e616d65225d, '1', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (3, 0x2275706c6f6164732f225b2266696c65225d5b226e616d65225d, '7', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (4, 0x2275706c6f6164732f225b226e616d65225d, '6', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (5, 0x282275706c6f6164732f22202e20245f46494c45535b2266696c65225d5b226e616d65225d29, '5', 'asd', '03.13.06');

weird eh? Here is the code for upload.html:

<html>
<head>
<title>Upload now!</title>
</head>

<form action="upload.php" method="post" enctype="multipart/form-data">
  <p>File Upload</p>
  <label for="file">File</label>
  <input type="file" name="file" id="file" /><br><br>
  <label for="titled">File Name</label><input type="text" name="titled"><br><br>
  <label for="contributedby">Contributed by:</label><input type="text" name="contributedby"<br><br>
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>
<body bgcolor="#FFFFFF">

</body>


</html>

here is the code for upload.php:

<?php
if (($_FILES["file"]["type"] == "image/gif" OR "images/jpg") &&
($_FILES["file"]["size"] < 1500000)) {
  echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
  echo "Uploading " . $_FILES["file"]["name"];
  echo " (" . $_FILES["file"]["type"] . ", ";
  echo ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";

  if (file_exists("uploads/" . $_FILES["file"]["name"])) {
    echo $_FILES["file"]["name"] . " already exists.  ";
    echo "Please delete the destination file and try again.";
  } else {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "uploads/" . $_FILES["file"]["name"]);

    echo "The file has been successfully uploaded!:D";

$imageurl = '("uploads/" . $_FILES["file"]["name"])';

$titled = $_REQUEST['titled'];
$contributedby = $_REQUEST['contributedby'];
$date = date("m.d.y");

$titled=strip_tags($titled);
$contributedby=strip_tags($contributedby);

$titled=addslashes($titled);
$contributedby=addslashes($contributedby);

$conn = mysql_connect("localhost","cortez","165745");

//now choose the database to use
mysql_select_db("cortez_photoalbums");

//Save the request in SQL syntax to a string
$request = "INSERT INTO images values(0,'".$imageurl."','".$titled."','".$contributedby."','".$date."')";

$results = mysql_query($request,$conn);

  }

} else
  echo "Sorry, we only accept .GIF, and .jpg images under 1500 Kb for upload.";
?>

I would love any assistance in solving this problem.

HoTDaWg

Handy links:
Google
PHP Manual


#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 13 March 2006 - 10:48 PM

$imageurl = '("uploads/" . $_FILES["file"]["name"])';

change to...

$imageurl = "uploads/" . $_FILES["file"]["name"];

php will not interpret the variable correctly in a single quote string (I see you concatenate but you still have a closing quote).
follow me on twitter @PHPsycho

#3 HoTDaWg

HoTDaWg
  • Members
  • PipPipPip
  • Advanced Member
  • 275 posts
  • LocationToronto

Posted 13 March 2006 - 11:01 PM

appreciate the help a lot, but according to my export code, my code still doesnt work:

-- phpMyAdmin SQL Dump
-- version 2.6.4-pl2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Mar 13, 2006 at 05:58 PM
-- Server version: 4.1.14
-- PHP Version: 4.3.11
-- 
-- Database: `cortez_photoalbums`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `images`
-- 

CREATE TABLE `images` (
  `id` int(15) NOT NULL auto_increment,
  `imageurl` blob NOT NULL,
  `titled` varchar(255) NOT NULL default '',
  `contributedby` varchar(22) NOT NULL default '',
  `date` varchar(50) NOT NULL default '0000-00-00',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;

-- 
-- Dumping data for table `images`
-- 

INSERT INTO `images` VALUES (1, 0x2275706c6f6164732f22202e20245f46494c45535b2266696c65225d5b226e616d65225d, '4', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (2, 0x2275706c6f6164732f225b2266696c65225d5b226e616d65225d, '1', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (3, 0x2275706c6f6164732f225b2266696c65225d5b226e616d65225d, '7', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (4, 0x2275706c6f6164732f225b226e616d65225d, '6', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (5, 0x282275706c6f6164732f22202e20245f46494c45535b2266696c65225d5b226e616d65225d29, '5', 'asd', '03.13.06');
INSERT INTO `images` VALUES (6, 0x75706c6f6164732f342e676966, '4', 'some_guy', '03.13.06');


thanks a lot though

Handy links:
Google
PHP Manual


#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,025 posts

Posted 13 March 2006 - 11:22 PM

If imageurl is a string value, why have you defined it as BLOB. Use varchar(255)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 HoTDaWg

HoTDaWg
  • Members
  • PipPipPip
  • Advanced Member
  • 275 posts
  • LocationToronto

Posted 14 March 2006 - 09:06 PM

thank you all so much for your help. The script is working Great! The only problem is I get the following error when using my latest page gallery.php:

Parse error: syntax error, unexpected T_STRING in /home/cortez/public_html/testscripts/photoalbums/gallery.php on line 24

Here is the script:

<html>
<head>
<title>Homework Administration- Delete Homework</title>
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
</head>
<body>
<?php

$conn = mysql_connect("localhost","cortez","165745");

//now choose the database to use
mysql_select_db("cortez_photoalbums");

$id = $_GET['id']; // retrieve id passed via URL
if (isset($id)) {
    $query = "DELETE FROM images WHERE id = '$id' ";
    $result = mysql_query($query);
}
$getimages = mysql_query("SELECT * FROM images ORDER BY id DESC");
while($r=mysql_fetch_array($getimages)) {
    extract($r); //remove the $r so its just $variable
    echo("<hr>
    Id: $id <br>
    Image: <image src="images/$imageurl" height="50" width="50"><br>
    Date: $date<br>
    <a href='". $_SERVER['PHP_SELF']. "?id=". $id. "'>delete</a>"); 
}
?>

</body>
</html>

I would be nowhere today if i had not found out about this site,

Hotdawg

PS: the reason I am adding the uploads/ in my image tag is because take a look at my database when I exported it:

-- phpMyAdmin SQL Dump
-- version 2.6.4-pl2
-- http://www.phpmyadmin.net
-- 
-- Host: localhost
-- Generation Time: Mar 14, 2006 at 03:50 PM
-- Server version: 4.1.14
-- PHP Version: 4.3.11
-- 
-- Database: `cortez_photoalbums`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `images`
-- 

CREATE TABLE `images` (
  `id` int(15) NOT NULL auto_increment,
  `imageurl` varchar(255) NOT NULL default '',
  `titled` varchar(255) NOT NULL default '',
  `contributedby` varchar(22) NOT NULL default '',
  `date` varchar(50) NOT NULL default '0000-00-00',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9;

-- 
-- Dumping data for table `images`
-- 

INSERT INTO `images` VALUES (1, '"uploads/" . $_FILES["file"]["name"]', '4', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (2, '"uploads/"["file"]["name"]', '1', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (3, '"uploads/"["file"]["name"]', '7', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (4, '"uploads/"["name"]', '6', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (5, '("uploads/" . $_FILES["file"]["name"])', '5', 'asd', '03.13.06');
INSERT INTO `images` VALUES (6, 'uploads/4.gif', '4', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (7, '2.gif', '2', 'some_guy', '03.13.06');
INSERT INTO `images` VALUES (8, '29.gif', 'some image', 'some guy', '03.14.06');

Handy links:
Google
PHP Manual


#6 HoTDaWg

HoTDaWg
  • Members
  • PipPipPip
  • Advanced Member
  • 275 posts
  • LocationToronto

Posted 14 March 2006 - 11:48 PM

so no one can help me?

Handy links:
Google
PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users