First draft of an image upload form
album
CREATE TABLE IF NOT EXISTS `album` (
`album_id` int(255) NOT NULL AUTO_INCREMENT,
`album_name` varchar(50) NOT NULL,
`img_name` varchar(500) CHARACTER SET utf8 NOT NULL,
`img_path` varchar(500) CHARACTER SET utf8 NOT NULL,
`img_type` varchar(500) CHARACTER SET utf8 NOT NULL,
`album_ident` varchar(500) NOT NULL,
PRIMARY KEY (`album_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
index.php
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="mystyle.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
</head>
<form method="POST" action="upload.php" enctype="multipart/form-data" class="ajax" />
<input type="file" name="file_img[]" multiple>
<input type="text" name="folder">
<input type="submit" value="Create directory" id="makeit">
</form>
<script>
$('form.ajax').on('submit', function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value){
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
var fd= new FormData(this)
$.ajax({
url:"upload.php",
type:"POST",
processData:false,
contentType: false,
data:fd,
success: function(response){
console.log(response);
$('.myDiv').html(response);
}
});
return false;
});
</script>
<body>
<div class="myDiv">
</div>
</body>
upload.php
<?php
error_reporting(E_ALL);
try{
$db = new PDO("mysql:host=localhost;dbname=mkdir", 'root', '');
$db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
echo $e->getMessage();
die(); }
if (isset($_FILES['file_img'])) {
$uploads = $_FILES["file_img"];
$folderName = $_POST["folder"];
$count = count($uploads['name']);
if (strlen($folderName) == "" && !is_null($folderName))
{
echo 'Album name is required';
}
else {
$length = 10;
$random = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
echo $folderName . ' has been created <br /> <br />';
}
if ($uploads['error'][0] === UPLOAD_ERR_NO_FILE){
$count = 0;
}
if ($count) { // files
foreach($uploads['name'] as $key => $name) {
if ($uploads['error'][$key] === UPLOAD_ERR_OK) {
$size = $uploads["size"][$key];
$type = $uploads["type"][$key];
$tmp = $uploads["tmp_name"][$key];
$name = $uploads["name"][$key];
$temp_dir = "uploads/" . $name;
if ($type != "image/jpeg" && $type != "image/png" && $type != "image/gif") {
echo $name . " Please upload jpg / png / gif <br />";
} else if ($size > 3145728) {
echo $name." is too large <br />";
} else if (!empty($name)) {
echo "Picture name ".$_FILES['file_img']['name'][$key]."<br />";
$sql = "INSERT INTO album (`album_name`, `img_name`, `img_path`, `img_type`, `album_ident`)
VALUES (:folder, :filename, :filepath, :filetype, '$random');
";
$query = $db->prepare($sql);
$query->execute(array(
':folder'=> $folderName,
':filename'=> $name,
':filepath'=> $temp_dir,
':filetype'=> $type
));
move_uploaded_file($tmp, "$temp_dir");
}
}
}
}else{
echo "<p>No files submitted</p>";
}
}
?>
Still relatively new to all of this, it works fine but I'm sure it could be improved upon.