Jump to content

Upload script with resize & Uploadify


arbitter

Recommended Posts

Hi there.

 

I'm making an album-upload-thingy. So far I've written the script that uploads an image, changes it's name, makes some directorys, makes a table, and inserts the image name in the table. Here's the script:

 

<?php
if(isset($_POST['addAlbum'])){
if(empty($_POST['naam']) || empty($_POST['urlnaam'])){
	$_SESSION['melding'] = 'U hebt niet alle verplichte velden ingevuld!';
	header('Location: ward/fotos');
	exit();
}
$naam = mysql_real_escape_string(htmlentities($_POST['naam']));
$urlnaam = mysql_real_escape_string(urlencode(str_replace(' ','',strtolower($_POST['urlnaam']))));
mysql_select_db('dyfemtaw_fotos');
$result = mysql_query("SELECT * FROM albums WHERE naam='$naam'")or die('result:' .mysql_error());
$result2 = mysql_query("SELECT * FROM albums WHERE urlnaam='$urlnaam'")or die('result2:'.mysql_error());
if(mysql_num_rows($result)==0 && mysql_num_rows($result2)==0){
	mysql_select_db('dyfemtaw_fotos');
	mysql_query("INSERT INTO albums (naam,urlnaam) VALUES ('$naam','$urlnaam')")or die('insert' .mysql_error());
	mysql_query("CREATE TABLE $urlnaam(
			`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
			`photoid` BIGINT NOT NULL ,
			`type` VARCHAR( 5 ) NOT NULL ,
			`description` VARCHAR( 200 ) NOT NULL)")or die('create: '.mysql_error());
	mkdir('fotos/'.$urlnaam);
	mkdir('fotos/'.$urlnaam.'/large');
	mkdir('fotos/'.$urlnaam.'/thumbs');
	//getting the extension of the file
		$filename = strtolower($_FILES['bestand']['name']);
		$exts = explode(".", $filename);
		$n = count($exts)-1;
		$exts = '.' . $exts[$n];
	$newname = rand(111111,999999) . time();
	$targetpath = 'fotos/' . $urlnaam . '/' . $newname . $exts;
	mysql_query("INSERT INTO $urlnaam (photoid,type) VALUES ('$newname','$exts')")or die(mysql_error());
	if(move_uploaded_file($_FILES['bestand']['tmp_name'], $targetpath)) {
		$_SESSION['melding'] = 'Het bestand ' . basename( $_FILES['bestand']['name']). " is geüpload.";
		header('Location: ward/fotos');
		exit();
	}else{
		$_SESSION['melding'] = "Er is een probleem opgetreden.";
		header('Loaction: ward/fotos');
		exit();
	}
}else{
	$_SESSION['melding'] = 'Deze naam is al gekozen.';
	header('Location: ward/fotos');
	exit();
}
}
?>

So what it does:

  • Check if the name of the database exists (name of the database is user inputted)
  • if not -> insert in the 'albums' table that album and it's name. From the urlname, make a new table
  • make directorys for the large (600px) files and the thumbnail (120px) files
  • get the extension of the file
  • make name of file with a random number and time()
  • insert the file name and the extension in the newly made table with the urlname of the album
  • move the file

 

So what I still need to do is resize the image twice; the large one (600px) should be stored in the album/large directory, the small one should be stored in the album/thumbs directory.

 

And then, I need to find a way to implement this to Uploadify, so I can allow multi-uploads :P

 

Also, I'm not quite sure my script is safe; I mean, it can make the database and still fail to upload the image, which ofcourse isn't good is it...

Link to comment
https://forums.phpfreaks.com/topic/255006-upload-script-with-resize-uploadify/
Share on other sites

The script seems to work good, but something rather weird is happening. This does work:

Resize_Image($fullname,$fullname,600,600,'fotos/'.$urlnaam.'/large/','fotos/'.$urlnaam.'/large/');

but this does not work:

Resize_Image($fullname,$fullname,120,120,'fotos/'.$urlnaam.'/large/','fotos/'.$urlnaam.'/thumbs/');

 

even though the thumbs folder does exist...

And another problem is: when you insert ' and " in the names ($_POST['naam'] and $_POST['urlnaam']), the values do get inserted into the 'albums' table, but there is no table made... Is there a function that strips things so they can be a table name? So I need something that cleans ANYTHING to something that can safely be used in a link and as a table name...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.