0

I am new to this PHP coding, but have experience. I am just wondering how to make it check the folder and not make it make duplicates of the file in the MySQL?

<?php

include('mysql.php');

if ($handle = opendir('images')) {

	while (false !== ($file = readdir($handle))) {
		if($file!='.' && $file!='..') {
			$images[] = "('".$file."')";
		}
	}

	closedir($handle);
}

$query = "INSERT INTO images (filename) VALUES ".implode(',', $images)." ";
if (!mysql_query($query)) {
	print mysql_error();
}
else {
	print "Images posted!";
}


?>
2
Contributors
13
Replies
14
Views
6 Years
Discussion Span
Last Post by diafol
0

I don't get what you're doing. Do you want to check other folders for duplicate files?

From the way you're doing this, you seem to be adding images to a single field with a ',' delimiter. This isn't the best way to save data. You'd be better off going down a relational path. This means you can search and update more effectively.

0

I don't get what you're doing. Do you want to check other folders for duplicate files?

From the way you're doing this, you seem to be adding images to a single field with a ',' delimiter. This isn't the best way to save data. You'd be better off going down a relational path. This means you can search and update more effectively.

How this work is there is a folder called, images, people can upload their photos in it. But it will not be picked up unless I use this script to lets just say reload the database. And everything I use this it duplicates in MySQL.

Example:

Photo > Upload >> Images Folder >>> Run this script >>>> Duplicate current photos in the images folder more than once in the MySQL database.

To add:
If their are 3 photos in the images folder, and I already ran this script before, but i run it again it will created about 3 more duplicates in the MySQL/

Edited by Rewired: n/a

0

When a user uploads, simply have the DB add this filename. No need to make a complete scan every time. Your problem, as I mentioned is your DB structure. It should be something like:

id | filename | date_uploaded | filesize | filetype | user_id | path

[id = PK, filename = actual filename in folder, date_uploaded = timestamp, filesize = size in KB or B, filetype = png/jpg/gif/svg etc, path = any subfolder path where file may be stored]

0

When a user uploads, simply have the DB add this filename. No need to make a complete scan every time. Your problem, as I mentioned is your DB structure. It should be something like:

id | filename | date_uploaded | filesize | filetype | user_id | path

[id = PK, filename = actual filename in folder, date_uploaded = timestamp, filesize = size in KB or B, filetype = png/jpg/gif/svg etc, path = any subfolder path where file may be stored]

Can you help me do this please this is my upload class:

<?php

 define ("MAX_SIZE","100"); 

 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if(isset($_POST['Submit'])) 
 {

 	$image=$_FILES['image']['name'];

 	if ($image) 
 	{

 		$filename = stripslashes($_FILES['image']['name']);

  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
 $size=filesize($_FILES['image']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}


$image_name=time().'.'.$extension;

$newname="images/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied) 
{
	echo '<h1>Copy unsuccessfull!</h1>';
	$errors=1;
}}}}


 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h1>File Uploaded Successfully!</h1>";
 }

 ?>
0

That looks like a good bit of code - I particularly like the change original name to time - as this should avoid overwrites. However, it is *possible* that two users could upload an image at the same time. In this case, you may wish to use microtime() or better still, join the unix timestamp and a random 6 figure number (mt_rand()). This may be overkill, but overwriting a file could spell disaster.

I'd keep the original name for display purposes.

Near the end of your code, I'd have a function call, something like:

echo addImageToDB($origfilename,$newfilename,$filesize,$ext);

Your function:

function addImageToDB($origfilename,$newfilename,$filesize,$ext){
//I assume your DB connection vars are placed previously - so just use them here
//sanitize your inputs with mysql_real_escape_string(htmlentities(...))
//I've placed a 's_' prefix on the variables to denote 'sanitized'
 
 $r = mysql_query("INSERT INTO images SET `origfile` = '$s_origfile',`newfile` = '$s_newfile',`filesize` = '$s_filesize',`ext`='$s_ext',`date_upload`=NOW() + 0");

 //check added correctly with mysql_affected_rows()
 //on the strength of this decide on the output ($output)

 return $output;
}

Edited by diafol: n/a

0

I did this right, right?

include('mysql.php');
echo addImageToDB($origfilename,$newfilename,$filesize,$ext);
function addImageToDB($origfilename,$newfilename,$filesize,$ext){
0

Looks OK to me. I usually place all my functions at the top after my includes before calling them - just helps me to keep the functions/regular code maintainable. Each to his own though.

include('mysql.php');
//you need to set your variables here - i.e. your original file code

echo addImageToDB($origfilename,$newfilename,$filesize,$ext);
function addImageToDB($origfilename,$newfilename,$filesize,$ext){
0

Something is not right it uploads to the images folder but didn't get set in the database. Did I do something wrong: (Upload Class Code)

<?php
include('mysql.php');
 define ("MAX_SIZE","100"); 

 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

 $errors=0;

 if(isset($_POST['Submit'])) 
 {

 	$image=$_FILES['image']['name'];

 	if ($image) 
 	{

 		$filename = stripslashes($_FILES['image']['name']);

  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
 if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
 		{
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
 $size=filesize($_FILES['image']['tmp_name']);


if ($size > MAX_SIZE*1024)
{
	echo '<h1>You have exceeded the size limit!</h1>';
	$errors=1;
}

$image_name=time().'.'.$extension;

$newname="images/".$image_name;

$copied = copy($_FILES['image']['tmp_name'], $newname); 
if (!$copied) 
{
	echo '<h1>Copy unsuccessfull!</h1>';
	$errors=1;
}}}}


 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h1>File Uploaded Successfully!</h1>";
 }
 echo addImageToDB($origfilename,$newfilename,$filesize,$ext);
function addImageToDB($origfilename,$newfilename,$filesize,$ext){

 $r = mysql_query("INSERT INTO images (filename) VALUES ` = '$s_origfile',`newfile` = '$s_newfile',`filesize` = '$s_filesize',`ext`='$s_ext',`date_upload`=NOW() + 0");


 return $output;
}

 ?>
0

You've no $output to output, but that shouldn't matter.
Your SQL looks a little suspect though. You're trying to place everything into the filename field. This is because you are using the VALUES syntax. I was using the SET syntax. They are not interchangeable.

0

You've no $output to output, but that shouldn't matter.
Your SQL looks a little suspect though. You're trying to place everything into the filename field. This is because you are using the VALUES syntax. I was using the SET syntax. They are not interchangeable.

Can I have your MSN so you can help me on Teamviewer?

0

> Can I have your MSN so you can help me on Teamviewer?

Sorry I don't have an MSN. My mum told me not to talk to strange men!

0

Oh....well can you edit my current code and put how it suppose to be please?

0

No - this is your project. I given you help (7 responses so far), so I think that's going far enough. You're almost there:

$r = mysql_query("INSERT INTO images (filename) VALUES ` = '$s_origfile',`newfile` = '$s_newfile',`filesize` = '$s_filesize',`ext`='$s_ext',`date_upload`=NOW() + 0");

This is the bit that's causing you grief. Work on it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.