file uploading is corrupted [error]

1. Data URIs represent the data of the image. put all the data URIs the user drags into the box into an array, and post it to a PHP file. This PHP file will process the URIs and upload them to the server.

$randomName = substr_replace(sha1(microtime(true)), ”, 12).’.’.$mime;
if(file_put_contents($uploaddir.$randomName, $decodedData)) {
echo $randomName.”:uploaded successfully”;
}
else {
// Show an error message should something go wrong.
echo “Something went wrong. Check that the file isn’t corrupted”;
}

reason: the folder which holds the uploaded file, has no permission to be written by any user.

resolution: chmod a+w uploadfolder/

2. “I know how to submit data into a mySQL table through a form. Now I want to upload the image file to the remote directory (say, ‘images/’) but at the same time save the file-name in the table. ”

Often you want a user to be able to upload a photo, but you don’t want to bog down your database space by saving all the images directly into the database. You instead save the image to your server, but keep a record in the database of what file was saved so you can easily reference the image when needed.

First let’s create a database:

CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30)) 

Creating a Form:

This is simply an HTML form that you would use to collect information to be added to the database.

<form enctype="multipart/form-data" action="add.php" method="POST"> 
 Name: <input type="text" name="name"><br> 
 E-mail: <input type="text" name = "email"><br> 
 Phone: <input type="text" name = "phone"><br> 
 Photo: <input type="file" name="photo"><br> 
 <input type="submit" value="Add"> 
 </form>

Processing the Data: add.php

<?php 
 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $email=$_POST['email']; 
 $phone=$_POST['phone']; 
 $pic=($_FILES['photo']['name']); 

 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 

 //Writes the information to the database 
 mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 {  
 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else {  
 //Gives and error if its not 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?> 
  • only allowing photo uploads, you might consider limiting the allowed file types to jpg, gif and png.
  •  check if the file already exists: not overwrite
  • simply rename each file with a unique ID

Uniqid (prefix, more_entropy)

  • prefix: what will be appended to the beginning of each ID.
  • more_entropy. If this is false or not specified it will return 13 characters, if it is true then 23 characters will be returned.
<?php

//creates a unique id with the ‘about’ prefix

$a = uniqid(about); echo $a;   echo “<br>”;

//creates a longer unique id with the ‘about’ prefix

$b = uniqid (about, true); Echo $b; echo “<br>”;

//creates a unique ID with a random number as a prefix – more secure than a static prefix

$c = uniqid (rand (),true); echo $c; echo “<br>”;

//this md5 encrypts the username from above, so its ready to be stored in your database

$md5c = md5($c); echo $md5c; ?>

crypt () In Action

<?php
$password = crypt('mypassword'); 
 print $password . “ is the encrypted version of mypassword”;
$password = crypt('mypassword' , 'd4'); print $password . " is the CRYPT_STD_DES version of mypassword<br>"; 
$password = crypt('mypassword' , 'k783d.y1g'); print $password . " is the CRYPT_EXT_DES version of mypassword<br>"; 
$password = crypt('mypassword' , '$1$d4juhy6d$'); print $password . " is the CRYPT_MD5 version of mypassword<br>"; 
$password = crypt('mypassword' , '$2a$07$kiuhgfslerd...........$'); print $password . " is the CRYPT_BLOWFISH version of mypassword<br>"; ?> 
output:
d4/qPbCcJ5tD. is the CRYPT_STD_DES version of mypassword
k7xEagYCDPPSc is the CRYPT_EXT_DES version of mypassword
$1$d4juhy6d$a.jIPYnvne1FWF2V6mGQR0 is the CRYPT_MD5 version of mypassword
$2a$07$kiuhgfslerd...........6k0kSI76CqJ/RWGnSp9MWRDF91gJZfW is the CRYPT_BLOWFISH version of mypassword
As long as you always use the same salt the encrypted password should always be the same, making it a good solution for password storage.

Testing uploaded Data

<?php 
 // Connects to your Database 
 mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ; 
 mysql_select_db("Database_Name") or die(mysql_error()) ; 

 //Retrieves data from MySQL 
 $data = mysql_query("SELECT * FROM employees") or die(mysql_error()); 

//Puts it into an array

while($info = mysql_fetch_array( $data )) {

//Outputs the image and other data

Echo "<img src=http://www.yoursite.com/images/".$info['photo'] ."> <br>";

Echo "<b>Name:</b> ".$info['name'] . "<br> ";

Echo "<b>Email:</b> ".$info['email'] . " <br>";

Echo "<b>Phone:</b> ".$info['phone'] . " <hr>";

} ?>

To show the image, we just use normal HTML for the image, and only change the last part (the actual image name) with the image name stored in our database.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: