PHP Upload Script

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2245 Views - Last Post: 07 August 2009 - 12:22 PM Rate Topic: -----

#1 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

PHP Upload Script

Posted 06 August 2009 - 01:43 PM

Hey Guys,

I've been trying to teach myself PHP and thought i would set myself a task of creating a gallery, but i have gotten to a hiccup with the upload script;

The image gets uploaded to the Directory but it doesn't save the file path etc.. in the database.

The code may not be very good as I'm just starting out and have been using different resources to complete this but if you guys could have a look to see where i'm going wrong then that would be great :)

<?php

// Enable sessions
session_start();

// Connect to database
$objConn1 = new COM("ADODB.Connection");
$objConn1->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ds/gallery.mdb;Persist Security Info=False");

// Check Admin is Logged in
if(empty($_SESSION['loggedInUser']))
{
	// Go to page
	header("Location: login.php");
	exit;
}

if(!empty($_GET) || !empty($_POST))
{
	// Upload File
	$success_File1 = false;
	if(!empty($_FILES['File1']['name']))
	{
		if(is_uploaded_file($_FILES['File1']['tmp_name']))
		{
			$uploaddir = "D:\home\C253134\client.iflintinternet.com/image/";
			
			// Set file name and upload path
			$File1name = $_FILES['File1']['name'];
			$uploadFile1 = $uploaddir.rawurlencode($File1name);
			$uploadFile1 = erreplace("%..", "", $uploadFile1);
			$uploadFile1 = str_replace("%", "", $uploadFile1);	
						
			$success_File1 = move_uploaded_file($_FILES['File1']['tmp_name'], $uploadFile1);
		}
		
	}
	// Run query
	$objConn1->Execute("INSERT INTO [Images]([Image_Name], [Image_Description], [Filename], [Path], [Display]) VALUES('".@$_POST['Name']."', '".@$_POST['Description']."', '".substr(strrchr($uploadFile1, "/"), 1)."', '".@$uploadFile1."', '".@$_POST['DropDown1']."')");
	// Go to page
	header("Location: admin.php");
	exit;
}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Upload Image</title> 
	<meta content="text/html; charset=iso-8859-1" http-equiv="Content-Type">
</head>
<body>
	<h2 align="center">Upload Image 
	</h2>
	<p align="center">
	</p>
	<form style="WIDTH: 100%" id="Form1" enctype="multipart/form-data" method="post" action="<?php echo  $_SERVER['PHP_SELF'].(@($_SERVER['QUERY_STRING'] > ' ') ? '?'.$_SERVER['QUERY_STRING'] : '') ?>">
		<p align="left">
			Image Name: 
			<input id="Name" name="Name">
			<br>
			Image Description: 
			<input id="Description" name="Description">
			<br>
			Display: 
		</p>
		<select id="DropDown1" name="DropDown1">
			<option selected="selected" value="Yes">Yes
			<option value="No">No
		</select>
		<br>
		<br>
		<input id="File1" type="file" name="File1">
		<br>
		<input id="Submit1" value="Upload" type="submit" name="Submit1">&nbsp; 
	</form>
</body>
</html>

<?php

// Close database connection
if(isset($objConn1)) @$objConn1->Close();


?>




cheers,

Tommyflint

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Upload Script

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: PHP Upload Script

Posted 06 August 2009 - 01:55 PM

Does it save anything at all in the database? Or is it just the file path that is missing?
Was This Post Helpful? 0
  • +
  • -

#3 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 02:00 PM

nothing whatsoever it just uploads the file and then reloads the page which is then blank it doesn't even forward you to the admin.php page which is what is intended

:S
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: PHP Upload Script

Posted 06 August 2009 - 02:07 PM

I would guess that the query simply fails and since it fails php does output a warning, it just doesn't show it to you because display_errors might be Off and since you call "exit;" you end up with a blank page and you don't get sent to the admin page since you have output before you send the header. :)
Was This Post Helpful? 0
  • +
  • -

#5 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: PHP Upload Script

Posted 06 August 2009 - 02:12 PM

Make sure to have

mysql_query(INSERT BLABLA) or die(mysql_error());


Was This Post Helpful? 0
  • +
  • -

#6 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: PHP Upload Script

Posted 06 August 2009 - 03:56 PM

actually thats when your using a mysql statements greg with ADODB i dont think it works like that but i could be wrong ...

after some quick googling i did see 2 different methods of error checking with ADODB

 // Run query
	$objConn1->debug = true;
	$query = "INSERT INTO [Images]([Image_Name], [Image_Description], [Filename], [Path], [Display]) VALUES('".@$_POST['Name']."', '".@$_POST['Description']."', '".substr(strrchr($uploadFile1, "/"), 1)."', '".@$uploadFile1."', '".@$_POST['DropDown1']."')";
	if ($objConn1->Execute($query) == false) { echo "ERROR WITH EXECUTE!!!"; }



or using a try catch block

 // Run query
	// this method required including the exceptions file to utilize this ...
	include("../adodb-exceptions.inc.php"); 
	try {
		$objConn1->Execute("INSERT INTO [Images]([Image_Name], [Image_Description], [Filename], [Path], [Display]) VALUES('".@$_POST['Name']."', '".@$_POST['Description']."', '".substr(strrchr($uploadFile1, "/"), 1)."', '".@$uploadFile1."', '".@$_POST['DropDown1']."')");
	} catch (exception $e) {
		var_dump($e);
	}



Im in no way proficient in ADODB except in minor .NET apps but maybe its worth a shot :-/
Was This Post Helpful? 0
  • +
  • -

#7 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 04:09 PM

Cheers for the help RPGonzo byt unfortunately it didn't work i still get a blank screen grr
Was This Post Helpful? 0
  • +
  • -

#8 gregwhitworth  Icon User is offline

  • Tired.
  • member icon

Reputation: 219
  • View blog
  • Posts: 1,604
  • Joined: 20-January 09

Re: PHP Upload Script

Posted 06 August 2009 - 04:11 PM

Quote

ADODB


Dangit. I feel like I have the beast by the tail and then I find out there is way more tail than I ever imagined.

What does that mean.

I notice he is using classes.

--

Greg
Was This Post Helpful? 0
  • +
  • -

#9 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 04:15 PM

Quote

Dangit. I feel like I have the beast by the tail and then I find out there is way more tail than I ever imagined.

What does that mean.

I notice he is using classes.


i'm confused :S lol
Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: PHP Upload Script

Posted 06 August 2009 - 04:17 PM

I did sugges the use of MySQL; Especially as this is to be created as a learning experience (as far as I'm aware). Like I stated before PHP and MySQL go hand-in-hand and this would probably be a much better (and more standardised) way to start learning database/SQL programming. Afterall, we love linux too (don't we? :P)
Was This Post Helpful? 0
  • +
  • -

#11 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 04:23 PM

Maybe Rudi's idea is best but still if anyone does find out what is wrong with this script then i would love to know.

On the other hand does anyone know any good tutorials they recommend for a PHP Gallery using MySQL one that i found that looked pretty good was: http://www.sitepoint...-system-minutes


tommyflint

Ohh thankyou to everyone who has tried helping me out so far cheers :)

This post has been edited by tommyflint: 06 August 2009 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#12 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: PHP Upload Script

Posted 06 August 2009 - 04:28 PM

Blank screen has me thinking your display errors may be disabled ... maybe try a simple change at the head of your file

ini_set('display_errors', 1);
error_reporting(E_ALL);



this would show EVERYTHING including notices...

Lol Mage i would have to agree though .. but if your learning might as well learn the "better" version of the mysql functions ...

http://us3.php.net/mysqli <- has prepared statements

http://us3.php.net/m.../book.mysql.php <- more simplex

This post has been edited by RPGonzo: 06 August 2009 - 04:34 PM

Was This Post Helpful? 0
  • +
  • -

#13 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 04:45 PM

View PostRPGonzo, on 6 Aug, 2009 - 10:28 PM, said:

Blank screen has me thinking your display errors may be disabled ... maybe try a simple change at the head of your file

ini_set('display_errors', 1);
error_reporting(E_ALL);



this would show EVERYTHING including notices...



I tried adding this but still get a black page :S

cheers for still trying :)
Was This Post Helpful? 0
  • +
  • -

#14 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: PHP Upload Script

Posted 06 August 2009 - 05:02 PM

Have you tried using odbc_connect and odbc_exec for this?

<?php

// Enable sessions
session_start();

// Connect to database
$connection = odbc_connect("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ds/gallery.mdb;Persist Security Info=False");


// Check Admin is Logged in
if(empty($_SESSION['loggedInUser']))
{
	// Go to page
	header("Location: login.php");
	exit;
}

if(!empty($_GET) || !empty($_POST))
{
	// Upload File
	$success_File1 = false;
	if(!empty($_FILES['File1']['name']))
	{
		if(is_uploaded_file($_FILES['File1']['tmp_name']))
		{
			$uploaddir = "D:\home\C253134\client.iflintinternet.com/image/";
			
			// Set file name and upload path
			$File1name = $_FILES['File1']['name'];
			$uploadFile1 = $uploaddir.rawurlencode($File1name);
			$uploadFile1 = erreplace("%..", "", $uploadFile1);
			$uploadFile1 = str_replace("%", "", $uploadFile1);	
						
			$success_File1 = move_uploaded_file($_FILES['File1']['tmp_name'], $uploadFile1);
		}
		
	}
	// Run query
	$query = "INSERT INTO [Images]([Image_Name], [Image_Description], [Filename], [Path], [Display]) VALUES('".@$_POST['Name']."', '".@$_POST['Description']."', '".substr(strrchr($uploadFile1, "/"), 1)."', '".@$uploadFile1."', '".@$_POST['DropDown1']."')");
	$result = odbc_exec($connection, $query);
	if($result)
		// Go to page
		header("Location: admin.php");
	else
		echo(odbc_errormsg);
	exit;
}

?>


Was This Post Helpful? 0
  • +
  • -

#15 tommyflint  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 34
  • View blog
  • Posts: 679
  • Joined: 24-July 08

Re: PHP Upload Script

Posted 06 August 2009 - 05:28 PM

Cheers PsychoCoder but for some reason that just doesn't show the upload form atall,

anyway i'm just following this tutorial and was wondering if someone could explain what this does i partially understand it:

$number_of_fields = (isset($GET['number_of_fields'])) ?
(int)($_GET['number_of_fields']) : 5


I understand it says the variable $number_of_fields = whatever the value of the Get variable is but i don't understand what the ? and etc do

sorry if i seem a bit of a noob but i'm really interested in learning this stuff and actually knowing what it does rather than just copy it into my PHP IDE from the tutorial.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2