Jump to content

How to upload an image, save it on the server and store its location into a mysq


Betty_S

Recommended Posts

Hallo everyone!

 

I wont to add php/html code  which can do that:

 

Let the user choose and upload a file (an image), save the file on the server and store its location into a mysql table.

 

If you have an example code or a link that would be super!

 

I know how to handle the html part but I have no idea whatsoever what to do with the path I get from the user!

 

Please help me.

 

Thanks.

 

Link to comment
Share on other sites

Here's a script where you can upload images to a folder, you just need to add the mysql part:

 

<?php
// ==============
// Configuration
// ==============
$uploaddir = "uploads"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777!
$allowed_ext = "jpg, gif, png, pdf"; // These are the allowed extensions of the files that are uploaded
$max_size = "50000"; // 50000 is the same as 50kb
$max_height = "100"; // This is in pixels - Leave this field empty if you don't want to upload images
$max_width = "100"; // This is in pixels - Leave this field empty if you don't want to upload images

// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}

// Check File Size
if ($ok == "1") {
if($_FILES['file']['size'] > $max_size)
{
print "File size is too big!";
exit;
}

// Check Height & Width
if ($max_width && $max_height) {
list($width, $height, $type, $w) = getimagesize($_FILES['file']['tmp_name']);
if($width > $max_width || $height > $max_height)
{
print "File height and/or width are too big!";
exit;
}
}

// The Upload Part
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']);
}
print "Your file has been uploaded successfully! Yay!";
} else {
print "Incorrect file extension!";
}
?>

 

Enjoy

Link to comment
Share on other sites

this may be use full to you ......

 



for($i=1;$i<=5;$i++) {
	if($_FILES["file$i"]['type'] != "") {
		$filename = $_FILES["file$i"];
		$filename = copy($filename, "../gallery/$dir_name/small/");
		$SQLQuery = "INSERT INTO files_pg (file_name, directory_id, directory_name) VALUES ('$filename', $dir_id, '$dir_name')";
		mysql_query($SQLQuery);
	}

 

 

~J

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.