Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a PHP Expert!

Join 244,161 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,353 people online right now. Registration is fast and FREE... Join Now!




Image upload script

2 Pages V  1 2 >  
Reply to this topicStart new topic

Image upload script, Need some help pleasee

JDogg2008
11 Jan, 2009 - 08:41 AM
Post #1

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
Hey guys, I'm trying to make a image uploader for my site with PHP, I'm a total beginner at PHP so bear with me. I want there to be a "recently uploaded" part, with 6 of the most recently uploaded images, is that possible?. Heres a mockup.
Mockup
Actually works biggrin.gif, IP logging works too, and the ban script, but my main problem is the recently uploaded thing, can anyone help?.

User is offlineProfile CardPM
+Quote Post


CTphpnwb
RE: Image Upload Script
11 Jan, 2009 - 09:07 AM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
QUOTE(JDogg2008 @ 11 Jan, 2009 - 08:41 AM) *
...can anyone help?.

Yes, but not without seeing your code.
User is offlineProfile CardPM
+Quote Post

Ändrew
RE: Image Upload Script
11 Jan, 2009 - 07:27 PM
Post #3

D.I.C Head
Group Icon

Joined: 21 Apr, 2008
Posts: 180



Thanked: 7 times
Dream Kudos: 75
My Contributions
lol fail JDogg its Donclurd here ill help you ASAP...
User is online!Profile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
11 Jan, 2009 - 08:33 PM
Post #4

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
QUOTE(Ändrew @ 11 Jan, 2009 - 07:27 PM) *

lol fail JDogg its Donclurd here ill help you ASAP...

Oki dokie.
Sory about not posting code crazy.gif.
Index.php:
CODE

<html>

<head>
  <title>CoDCreators~Image Uploader</title>

<style>

body{
  font-family: Tahoma;
  font-size: 11px;
  color: #EAEAEA;
}

</style>

</head>

<body bgcolor="#252525">

<center>

<div style="margin-top: 100px;">

<div style="background: #1e1e1e; border: 4px solid #7c7360; padding: 20px; width: 500px;" >
<center><img src="http://www.iconspedia.com/uploads/762332078.png"></img></center>
Use the tool below to upload images, maximum size is 3mb's, Jpg,gif and bmp are the only supported formats.
<h2>Sig hosting - CoDCreators.com</h2>
<form action="upload_ac.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
Select file
<input name="ufile" type="file" id="ufile" size="50" /></td>
<input type="submit" name="Submit" value="Upload" />
<br>
<hr></hr>
<h2>Recently uploaded</h2>
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "files/".$HTTP_POST_FILES['ufile']['name'];
echo "<img src=\"$path/latest.jpg\" width=\"150\" height=\"150\">";
echo "<img src=\"$path/Blue hills.jpg\" width=\"150\" height=\"150\">";
echo "<img src=\"$path/Water lilies.jpg\" width=\"150\" height=\"150\">";
echo "<img src=\"$path/Winter.jpg\" width=\"150\" height=\"150\">";
echo "<img src=\"$path/me.jpg\" width=\"150\" height=\"150\">";
echo "<img src=\"$path/1-1.jpg\" width=\"150\" height=\"150\">";
echo "<br>Do NOT post any pornographic images, it will be deleted immediately, your IP is being logged."

?>
<?php
$IP = getenv("REMOTE_ADDR");
echo " IP:".$IP;
?>
<?php
$banip = array('empty','empty','empty');

$ip = $_SERVER['REMOTE_ADDR'];

for($i=0;$i<3;$i++){

if($ip == $banip[$i])
{
echo "You are banned";
?>
<html><script type="text/javascript">
location.replace("banned.php");
</script>
</html>
<?php
}
else
{
echo "<br>You are not banned.";
}
}
?>
</div>
<br /><center><font size="1">Created by JDogg | www.JDogg.CodCreators.com</font></center>
</div>

</center>

</body>
</html>
<!--
To Do:
~Recently uploaded thing.
~Log the file name in the log.html.
~Show eXsolved!.
--!>

CODE

upload_ac.php
<html>

<head>
  <title>Sig/Image Hosting</title>

<style>

body{
  font-family: Tahoma;
  font-size: 11px;
  color: #EAEAEA;
}

</style>

</head>

<body bgcolor="#252525">

<center>

<div style="margin-top: 100px;">

<div style="background: #1e1e1e; border: 4px solid #7c7360; padding: 20px; width: 500px;" >

<h2>Sig hosting - CoDCreators.com</h2>
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "files/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
echo "<a href=\"index.php\">Back</a>";
}
else
{
echo "Error";
}
}
?>
<?php
$IP = getenv("REMOTE_ADDR");
echo "<br> IP:".$IP;
?>
<?php
$logfile= '/home1/codcreat/public_html/jdogg/upload/log.html';
$IP = $_SERVER['REMOTE_ADDR'];
$logdetails=  date("F j, Y, g:i a") . ': ' . '<a href=http://dnsstuff.com/tools/city.ch?ip='.$_SERVER['REMOTE_ADDR'].'>'.$_SERVER['REMOTE_ADDR'].'</a>';
$fp = fopen($logfile, "a");
fwrite($fp, $logdetails);
fwrite($fp, "<br>");
fclose($fp);
?>
<br><br>

</div>
</div>

</center>

</body>
</html>


This post has been edited by JDogg2008: 11 Jan, 2009 - 08:34 PM
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Image Upload Script
11 Jan, 2009 - 09:28 PM
Post #5

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
I'd set up an array with those six file paths and then use array_shift() when ever adding a new file.

http://www.php.net/manual/en/function.array-shift.php


User is offlineProfile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
11 Jan, 2009 - 09:33 PM
Post #6

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
QUOTE(CTphpnwb @ 11 Jan, 2009 - 09:28 PM) *

I'd set up an array with those six file paths and then use array_shift() when ever adding a new file.

http://www.php.net/manual/en/function.array-shift.php

Thanks I'll give that a read and try it out.
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Image Upload Script
11 Jan, 2009 - 09:54 PM
Post #7

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
Note that you'll also need to store the array somewhere between posts. You could use a database or a session variable.

http://www.php.net/session

User is offlineProfile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
11 Jan, 2009 - 10:22 PM
Post #8

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
QUOTE(CTphpnwb @ 11 Jan, 2009 - 09:54 PM) *

Note that you'll also need to store the array somewhere between posts. You could use a database or a session variable.

http://www.php.net/session

Which is easier? A database or a session variable?.
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Image Upload Script
12 Jan, 2009 - 04:47 AM
Post #9

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
A session variable:
CODE
<?php
session_start();
$myarray = $_SESSION['myarray'];
//
//Your code, which may alter $myarray.
//
$_SESSION['myarray']=$myarray;
?>

User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Image Upload Script
12 Jan, 2009 - 09:05 AM
Post #10

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 467



Thanked: 49 times
Dream Kudos: 75
My Contributions
I'd store the data into a database, and if you wanted immediate front end integrationon-the-fly as you're uploading, you could impliment AJAX to echo out the latest '6' as you go along.

To do this, insert the file data into your database table, along with a timestamp. Then to show the 'latest six downloads/uploads' you can pull the results using a query like:

CODE

SELECT * FROM tbldocs ORDER BY timestamp LIMIT 6


This eliminates the need to post serialized arrays / ids etc and will still be there the next time you log on to the application, unlike a $_SESSION or $_POST data
User is offlineProfile CardPM
+Quote Post

CTphpnwb
RE: Image Upload Script
12 Jan, 2009 - 09:37 AM
Post #11

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 1,570



Thanked: 94 times
Dream Kudos: 100
Expert In: PHP

My Contributions
QUOTE(pemcconnell @ 12 Jan, 2009 - 09:05 AM) *

I'd store the data into a database, and if you wanted immediate front end integrationon-the-fly as you're uploading, you could impliment AJAX to echo out the latest '6' as you go along.

Yes, that would probably best. I wasn't thinking about the need to keep track of the array between sessions. Of course, if you don't have a database you could write to a text file. It is only six lines. wink2.gif
User is offlineProfile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
12 Jan, 2009 - 03:44 PM
Post #12

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
Thanks guys!.
User is offlineProfile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
13 Jan, 2009 - 12:33 AM
Post #13

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
Ok I've set up my databases and everything, but I get this error when I go to upload.
CODE

Warning: mysql_query() [function.mysql-query]: Access denied for user 'codcreat'@'localhost' (using password: NO) in /home1/codcreat/public_html/jdogg/upload/upload_ac.php on line 86

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home1/codcreat/public_html/jdogg/upload/upload_ac.php on line 86
Bann List Unavalible

My username isnt codcreat and my password isnt NO.
This is my config.php
CODE
<?php
$dbhost = 'localhost';
$dbuser = 'codcreat_jdogg2';
$dbpass = '******';
$dbname = 'main';
?>

CODE

<html>

<head>
  <title>Sig/Image Hosting</title>

<style>

body{
  font-family: Tahoma;
  font-size: 11px;
  color: #EAEAEA;
}

</style>

</head>

<body bgcolor="#252525">

<center>

<div style="margin-top: 100px;">

<div style="background: #1e1e1e; border: 4px solid #7c7360; padding: 20px; width: 500px;" >

<h2>Sig hosting - CoDCreators.com</h2>
<?php
//set where you want to store files
//in this example we keep file in folder upload
//$HTTP_POST_FILES['ufile']['name']; = upload file name
//for example upload file name cartoon.gif . $path will be upload/cartoon.gif
$path= "files/".$HTTP_POST_FILES['ufile']['name'];
if($ufile !=none)
{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "Successful<BR/>";

include "config.php";
include "opendb.php";

$name = 'name';
$size =  'size';
$createddate = date("F j, Y, g:i a");


$query = "INSERT INTO uploads (name, size, createddate)
   VALUES ('$name', '$size', '$createddate')";
  
mysql_query($query) or die('Upload completed but its not in the registry.');

include "closedb.php";

//$HTTP_POST_FILES['ufile']['name'] = file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "File Name :".$HTTP_POST_FILES['ufile']['name']."<BR/>";
echo "File Size :".$HTTP_POST_FILES['ufile']['size']."<BR/>";
echo "File Type :".$HTTP_POST_FILES['ufile']['type']."<BR/>";
echo "<img src=\"$path\" width=\"150\" height=\"150\">";
echo "<a href=\"index.php\">Back</a>";
}
else
{
echo "Error";
}
}
?>

<?php
$IP = getenv("REMOTE_ADDR");
echo "<br> IP:".$IP;
?>

<?php
include "config.php";
include "opendb.php";

$name = 'name';
$createddate = date("F j, Y, g:i a");
$IP = $_SERVER['REMOTE_ADDR'];

$query = "INSERT INTO bannlist (id, ip, reason, createddate)
   VALUES ('$id', '$ip', '$createddate', '$reason')";
   include "closedb.php";
    mysql_query($query) or die('Bann List Unavalible');

?>
<br><br>

</div>
</div>

</center>

</body>
</html>

^upload_ac.php

This post has been edited by JDogg2008: 13 Jan, 2009 - 12:35 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Image Upload Script
13 Jan, 2009 - 01:57 AM
Post #14

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,437



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
The problem is (in my opinion) that you have not yet connected to the database in your php code.
CODE
<?php
$dbhost = 'localhost';
$dbuser = 'codcreat_jdogg2';
$dbpass = '******';
$dbname = 'main';

$cid = mysql_connect($dbhost,$dbuser,$dbpass);
?>


CODE

...
<?php
...
include "config.php";
if($cid) {
$query = "INSERT INTO uploads (name, size, createddate)
   VALUES ('$name', '$size', '$createddate')";
  
mysql_query($query) or die('Upload completed but its not in the registry.');
}
else die("No Database Connection");

User is online!Profile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
13 Jan, 2009 - 02:23 AM
Post #15

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
It's not connecting to the database.
CODE
ErrorUpload completed but its not in the registry.

User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Image Upload Script
13 Jan, 2009 - 02:26 AM
Post #16

Unix Ronin
Group Icon

Joined: 10 May, 2007
Posts: 10,437



Thanked: 198 times
Dream Kudos: 2725
Expert In: Goofing Off

My Contributions
QUOTE(JDogg2008 @ 13 Jan, 2009 - 04:23 AM) *

It's not connecting to the database.

Replicate exactly what you expect it to be doing in code, at the command line. Make sure the values you expect to work do in fact work.
User is online!Profile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
13 Jan, 2009 - 02:33 AM
Post #17

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
QUOTE(no2pencil @ 13 Jan, 2009 - 02:26 AM) *

QUOTE(JDogg2008 @ 13 Jan, 2009 - 04:23 AM) *

It's not connecting to the database.

Replicate exactly what you expect it to be doing in code, at the command line. Make sure the values you expect to work do in fact work.

I don't know what you mean sorry. Like a sudo code?.
CODE
Upload Image.jpg, if the upload is completed submit ip, size of image.jpg, name of image.jpg, the date and time image.jpg was created, and the id of image.jpg

User is offlineProfile CardPM
+Quote Post

pemcconnell
RE: Image Upload Script
13 Jan, 2009 - 07:30 AM
Post #18

D.I.C Regular
Group Icon

Joined: 5 Aug, 2008
Posts: 467



Thanked: 49 times
Dream Kudos: 75
My Contributions
change this line:

CODE

mysql_query($query) or die('Upload completed but its not in the registry.');


to this:

CODE

mysql_query($query) or die('Upload completed but its not in the registry. Reason:<br/>'.mysql_error().'<br/>SQL:<br/>'.$query);


And let us know what it says

This post has been edited by pemcconnell: 13 Jan, 2009 - 07:30 AM
User is offlineProfile CardPM
+Quote Post

genesis1989
RE: Image Upload Script
13 Jan, 2009 - 05:00 PM
Post #19

New D.I.C Head
*

Joined: 30 Dec, 2008
Posts: 16

QUOTE(JDogg2008 @ 11 Jan, 2009 - 08:41 AM) *

Hey guys, I'm trying to make a image uploader for my site with PHP, I'm a total beginner at PHP so bear with me. I want there to be a "recently uploaded" part, with 6 of the most recently uploaded images, is that possible?. Heres a mockup.
Mockup
Actually works biggrin.gif, IP logging works too, and the ban script, but my main problem is the recently uploaded thing, can anyone help?.


Hey JDogg..... well m new to but as i see no code.... i see pitty and thought of helping out! I have completed the part of uploading images to a mysql database... hope this helps if it does drop me a message! i Have attached 2 php files that would do the trick! smile.gif


Attached File(s)
Attached File  uploadImages.php ( 3.87k ) Number of downloads: 5
Attached File  imageDisplay.php ( 838bytes ) Number of downloads: 2
Attached File  listPhotos.php ( 848bytes ) Number of downloads: 4
User is offlineProfile CardPM
+Quote Post

JDogg2008
RE: Image Upload Script
13 Jan, 2009 - 07:42 PM
Post #20

New D.I.C Head
*

Joined: 11 Jan, 2009
Posts: 23


My Contributions
Thanks alot Genesis, I'll give that code a go. But the error I got was:
CODE

Reason:
No database selected
SQL:
INSERT INTO uploads (name, size, createddate) VALUES ('', '', '')

If anyone has the time, I've uploaded my entire upload directory.

This post has been edited by JDogg2008: 13 Jan, 2009 - 07:44 PM


Attached File(s)
Attached File  JDogg.zip ( 4.52k ) Number of downloads: 3
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 04:56AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month