DropzoneJs with basic php form - php

I need to post form inputs and upload img same form.
php form
<form action="upload.php" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label for="exampleFormControlInput1">Add Image DropzoneJs</label>
<input type="text" class="form-control" id="yorum_name" placeholder="Nickname" name ="yorum_name">
</div>
<div class="form-group">
<input class="form-control" type="text" id ="yorum_title" name ="yorum_title" placeholder="Title" />
</div>
<div class="form-group">
<textarea class="form-control" name="yorum_text" id="yorum_text" placeholder="Text..."></textarea>
</div>
<div class="dropzone" id="myDropzone" name="yorum_img" id="yorum_img"></div>
<button class="btn btn-primary btn-lg btn-block" name="yorumekle" type="submit" id="submit-all"> upload </button>
</form>
custom.js file
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("div#myDropzone", { url: "upload.php"});
upload.php file
<?php
print_r($_POST['yorumekle']);
exit;
if(!empty($_FILES)){
// Include the database configuration file
require 'dbConfig.php';
// File path configuration
$targetDir = "uploads/";
$fileName = $_FILES['file']['name'];
$targetFilePath = $targetDir.$fileName;
// Upload file to server
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetFilePath)){
if (isset($_POST['yorumekle'])) {
$kaydet=$db->prepare("INSERT INTO yorum SET
yorum_title = :yorum_title,
yorum_img = :yorum_img,
yorum_text = :yorum_text,
yorum_name = :yorum_name
");
$insert=$kaydet->execute(array(
'yorum_title' => $_POST['yorum_title'],
'yorum_img' => $_POST['yorum_img'],
'yorum_text' => $_POST['yorum_text'],
'yorum_name' => $_POST['yorum_name']
));
}
// Insert file information in the database
$insert = $db->query("INSERT INTO files (file_name, uploaded_on) VALUES ('".$fileName."', NOW())");
}
}
?>
I add to tag jquery library & dropzone libraries. If you know more simply way to do this or another library like dropzoneJs i will try.
custom js not working know please help me.

Related

Image upload doesn't work and I also don't get an error

I am trying to upload an image file to my server using some code I found on the internet but it doesn't work and I can't find out why.
<form method="post" action="?p=edit-profil&id=<?php echo($user_id); ?>">
<h2>Profil bearbeiten</h2>
<hr />
<h4>Profilbild ändern</h4>
<p style="margin-top: 5px;">(Zulässige Dateiformate: .png .jpg)</p>
<input type="hidden" name="size" value="1000000" style="padding: 6px;">
<div>
<input type="file" name="image" class="choose-image">
</div>
<div>
<input type="submit" name="add-personal-data" value="Speichern" class="submitbutton">
</div>
</form>
// Get image name
$image = $_FILES['image']['name'];
// image file directory
$target = "img/".basename($image);
$image_insert = 'img/'.$image;
$image_upload_statement = $pdo->prepare("UPDATE beschreibung SET profilbild = '$image_insert' WHERE user_id = '$user_id'");
$image_upload_statement->execute();
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
So usually image_insert should be someting like img/picture.png but I only get img/ and there is no upload aswell. Since I am quit new with working with this topic I have no more idea how to fix the problem.
Your form tag should have this attribute enctype="multipart/form-data" to be able to send the image to the php file.

File upload in PHP not working?

My file upload system just isn't working properly, it doesn't return true when I call move_uploaded_file(). Here is my code, not sure if i'm just blind:
HTML:
<form action="updatecheat.php" method="POST">
<label for="version">Version:</label>
<input type="text" class="form-control" id="version" name="version" placeholder="New Version Number" />
<input type="hidden" name="referrer" id="referrer" value="coven-updates.php" />
<div class="custom-file-upload">
<label for="covenupdate">Upload "Coven.exe"</label>
<input type="file" id="covenupdate" name="covenupdate" />
</div>
<br /><br />
<button type="submit" name="submit" id="submit" class="btn btn-success waves-effect waves-light m-r-10">Update</button>
</form>
PHP:
$errors= array();
$file_name = $_FILES['covenupdate']['name'];
$file_size =$_FILES['covenupdate']['size'];
$file_tmp =$_FILES['covenupdate']['tmp_name'];
$file_type=$_FILES['covenupdate']['type'];
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){
$fn = "../Coven/Utilities/Version.txt";
$file = fopen($fn, "w+");
$size = filesize($fn);
fwrite($file, $_POST['version']);
$text = fread($file, $size);
fclose($file);
header("Location: urlhere");
} else {
header("Location: urlhere");
}
I have no clue why it isn't uploading properly. Any help is appreciated!
Thanks!
With your form tag you are missing one attribute enctype='multipart/form-data' which is must while you are working with upload file.
So, just change your form tag to this:
<form action="updatecheat.php" method="POST" enctype='multipart/form-data'>
you need to include enctype for a file ulpload to work .i.e enctype="multipart/form-data"
Try with add attribute enctype='multipart/form-data' and
File will be stored in temporary location, use tmp_name instead of name
if(move_uploaded_file($file_tmp,"../Coven/Utilites/Update/".$file_name)){

Dynamically adding input file object with form

I designed a form in form.php with a file input. When I press +input button, a new file input object should be generated and save them to my mysql server.
My code here,
form.php
<div id="enterReplyReferenceList">
<div class="enterReplyRefInputContainer">
<div class="enterReplyRefInput">
<form action="submit.php" method="POST" enctype="multipart/form-data">
<input type="hidden" value="<?php echo $userID; ?>" name="userID">
<input type="submit" value="SEND"/>
<input type="file" name="enterRefFile[]" multiple class="enterRefFile" accept="application/pdf,image/jpeg, image/png, image/jpg"/>
</form>
</div>
</div>
</div>
<button onclick="appendRef()">+input</button>
form.js
function appendRef(){
var inputclass = document.getElementsByClassName('enterReplyRefInputContainer')[0];
var inputclassChild = document.createElement('div');
inputclassChild.innerHTML = inputclass.innerHTML;
var newClass = document.getElementById("enterReplyReferenceList").appendChild(inputclassChild);
newClass.className = "enterReplyRefInputContainer";
submit.php
for($i=0; $i<count($_FILES['enterRefFile']['name']); $i++) {
$tmpFilePath = $_FILES['enterRefFile']['tmp_name'][$i];
if ($tmpFilePath != ""){
$fileData = addslashes(file_get_contents($_FILES['enterRefFile']['tmp_name'][$i]));
$fileName = addslashes($_FILES['enterRefFile']['name'][$i]);
$fileType = $_FILES['enterRefFile']['type'][$i];
if($fileData != null){
if($fileType == "application/pdf" || $fileType == "image/png" || $fileType == "image/jpeg" || $fileType == "image/jpg"){
mysql_query("INSERT INTO attachment (attachData, dataType, attachName, userID) VALUES('$fileData','$fileType','$fileName', '$userID')");
}
}
}
}
When I pressed the +input button, a new input file object is generated with the same look with the original and I can select the files. However, only a single file can be uploaded to my mysql db after I clicked the submit button. Can somebody tell me why the new file input cannot be uploaded? Is the new objects will not be sent?
The problem is that your input button is cloning your whole div, which has the form, so when you click "Submit" you're uploading one form with one file element. Here's an updated version (all in one file, you can separate to your liking):
<button onclick="appendRef()">+input</button><br><hr>
<form action="submit.php" method="POST" enctype="multipart/form-data">
<input type="hidden" value="<?php echo $userID; ?>" name="userID">
<div id="enterReplyReferenceList">
<div class="enterReplyRefInputContainer">
<div class="enterReplyRefInput">
<input type="file" name="enterRefFile[]" multiple class="enterRefFile" accept="application/pdf,image/jpeg, image/png, image/jpg"/>
</div>
</div>
</div>
<hr>
<input type="submit" value="SEND"/>
</form>
<script>
function appendRef(){
var inputclass = document.getElementsByClassName('enterReplyRefInputContainer')[0];
var inputclassChild = document.createElement('div');
inputclassChild.innerHTML = inputclass.innerHTML;
var newClass = document.getElementById("enterReplyReferenceList").appendChild(inputclassChild);
newClass.className = "enterReplyRefInputContainer";
}
</script>
Button click is outside of the form so you don't do a submit action on it (unless you want to use a blocking action like jQuery preventDefault(), and you only need one submit for the whole form.

Image upload issue Laravel 4 - File is being corrupted

I am trying to upload an image using laravel4 and I've successfully managed to upload a file from a form and store it in a directory of my choice in the public folder. However the issue I am running into is viewing the images. I am developing in Netbeans and when I try to open my newly uploaded image I am told the image is corrupted.
This is my form:
<form action="{{ action('BookController#handleCreate') }}" method="post" role="form" enctype="multipart/form-data">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" />
</div>
<div class="form-group">
<label>Description</label>
<input type="textarea" class="form-control" name="desc" />
</div>
<!-- Img upload -->
<input type="file" name="img"/>
<input type="submit" value="Create" class="btn btn-primary" />
Cancel
</form>
This is my controller:
public function handleCreate(){
$book = new Book;
$book->book_name = Input::get('name');
$book->book_desc = Input::get('desc');
$destinationPath = public_path().'/uploads/covers/';//Set up destination path
$file = Input::file('img');//Get the file
$extension = $file->getClientOriginalExtension();//Get the extension
$filename = str_random(12).".".$extension;//Create a filename
$upload_success = $file->move($destinationPath, $filename);//Move the file to the destination
$pathToFile = '/uploads/covers/'.$filename;
echo $pathToFile;
//If successful.....
if($upload_success){
$book->book_cover = $filename;//store value in db
$book->save();//Save the book
return Redirect::action('BookController#index');
}else{//Else return to form with an error message....
return Response::json('error', 400);
}
}
This is the path to my image
/uploads/covers/VFBDJPqEdI6P.jpg
My plan is that I will store this path in the database and use it to display the image on the screen.
I don't know why but whilst I am uploading the files they are being corrupted and I'd appreciate if someone might suggest a way I could fix this.
Regards
I tried this and worked without losing any content
Storage::put($pathToFile,file_get_contents($request->file('img')->getRealPath()));

My $_FILES['file']['name'] is producing gibberish

I am uploading files with the following code using Bootstrap as my front-end framework.
<form method="POST" enctype="multipart/form-data" action= "php/up-load.php" role="form"
<div class="form-group">
<label for="file" class="col-sm-5 control-label">
Select file(Compressed format)
</label>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000000"/>
<input type="file" id="file" name="file" accept=".zip, .rar"/>
</div>
</div>
<div>
<button type="submit" class="btn btn-primary" name="upload" id="upload">Send</button>
</div>
</form>
My php code is;
# $original=$_FILES['file']['name'];
# $kiss=pathinfo($original, PATHINFO_EXTENSION);
$allowed_extensions = array(".zip","rar","bzip2","iso","gz","rz","7z","tar","tgz","bz2","tbz2","lzma","tlz");
$result = in_array ("$kiss", $allowed_extensions);
if (!$result)
{
// Wrong file type
}
else
{
//proceed..
}
My problem is that the
$_FILES['file']['name'];
is returning gibberish such as php7vy8X9 and phpY8wQVR . Have tried everything. What could be the problem?
$_FILES["file"]["name"] should be returning the original name of your file, what you are reporting should be stored inside $_FILES["file"]["tmp_name"]
The whole $_FILES array should look like this:
echo $_FILES["file"]["name"]; // Original Name
echo $_FILES["file"]["tmp_name"]; // Name (and location) given to the file by PHP
echo $_FILES["file"]["type"]; // File/mime type
echo $_FILES["file"]["size"]; // Total bytes
echo $_FILES["file"]["error"]; // Any error code which is returned during transfer

Categories