move_uploaded_file( ) not working on video/audio/pdf - php

I've done an upload page that should upload the files and set their name in the database. It works just perfect with the pictures , but the sound formats and the other ones doesn't seem to work.
This is how my html part look
<form method="post" enctype="multipart/form-data">
<div class="card card-login">
<?= FH::csrfInput() ?>
<div class="card-header text-center" data-background-color="rose" >
<h3 class="card-title">Upload</h3>
</div>
<div class="card-content">
<div class="input-group">
<span class="input-group-addon">
</span>
<div class="form-group label-floating">
<label class="control-label"><h4>Chose a name for the file</h4></label>
<br>
<input type="textd" name="name" id="name" class="form-control" value="">
</div>
<br><br>
<div class="form-group label-floating">
<label class="control-label"><h4>Choose a file</h4></label>
<br>
<input type="file" id="file" name="file" >
</div>
</div>
</div>
<div class="footer text-center">
<div class="file-upload">
<label for="submit" class="file-upload__label">
<div class="isa_error_class">
<?= FH::displayErrors($this->displayErrors)?>
</div>
<button class="btn btn-wd btn-lg" data-background-color="rose">Submit</button>
</label>
<input type="submit" name="submit" value="Submit" class="file-upload__input">
</div>
</div>
</form>
And there is the php part
if($this->request->isPost())
{
$this->request->csrfCheck();
$upload->assign($this->request->get());
$upload->user_id = Users::currentUser()->id;
$upload->name .= "." . pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$value = pathinfo($_FILES['file']['name'] , PATHINFO_EXTENSION);
$upload->format = Upload::setFormat($value);
$dir = Users::currentUser()->id;
if(move_uploaded_file($_FILES["file"]["tmp_name"],'files' . DS . $dir . DS . $upload->name ))
{
if($upload->save())
{
Router::redirect('upload');
}
else
{
$upload->addErrorMessage('file','There were a problem saving in the database.');
}
}
else
{
$upload->addErrorMessage('file','There were a problem uploading it.');
}
}
The DS is the separator. The image formats seems to work perfect , but the other formats don't. Any ideas ?

You should check if u have allowed file_uploads = On in your php.ini and also check the maximum file size upload_max_filesize= 20M and to make sure that you are not passing it.

Related

Uploading image dont have an extension

I created a form and am trying to upload image to directory and add the path in database. Hence, i submit the form only the image filename gets submitted without file extension name and also it wont be uploaded in the directory.
Below is my php code
if(isset($_POST['addcourse'])) {
$uploadDirectory = "images/";
if (!isset($_FILES['passport']) || !is_uploaded_file($_FILES['passport']['tmp_name'])) {
$passport_data = $_FILES['passport']['tmp_name']; //file input
// $passport_name = $_FILES['passport']['name']; //unique id for random filename
$passport_size = $_FILES['passport']['size'];
$unique_name = uniqid();
$extension = pathinfo($_FILES['passport']['name'], PATHINFO_EXTENSION);
// $target_file = $uploadDirectory.basename($_FILES['passport']['name']);
$target_file = $uploadDirectory . basename($unique_name . "." . $extension);
$passport_name = $unique_name . "." . $extension;
move_uploaded_file($passport_data, $target_file);
$name = mysqli_escape_string($myConn, $_POST["name"]);
$code = mysqli_escape_string($myConn, $_POST["code"]);
$course_req = mysqli_escape_string($myConn, $_POST["course_req"]);
$course_des = mysqli_escape_string($myConn, $_POST["course_des"]);
$sql = "INSERT INTO courses (name,code,course_req,course_des,passport)
VALUES ('$name','$code','$course_req','$course_des','$passport_name')";
if (mysqli_query($myConn, $sql)) { ?>
<script type="text/javascript">
alert("New Course Created Successfully");
window.location = "addcourse";
</script>
<?php } else {
echo "Error: " . $sql . ":-" . mysqli_error($myConn);
}
mysqli_close($myConn);
}
}
?>
Below is the form i created
<form class="form-horizontal" action="insert" method="post" role="form" enctype="multipart/form-data">
<div class="form-group row">
<label class="control-label col-sm-3 align-self-center">Course Name:</label>
<div class="col-sm-9">
<input type="text" name="name" class="form-control" id="course" placeholder="Enter Course Name">
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3 align-self-center">Course Code:</label>
<div class="col-sm-9">
<input type="text" name="code" class="form-control" id="code" placeholder="Enter Course Code">
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3 align-self-center">Course Requirements:</label>
<div class="col-sm-9">
<textarea type="text" name="course_req" class="form-control" id="course_req" rows="3"></textarea>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3 align-self-center">Course Description:</label>
<div class="col-sm-9">
<textarea type="text" name="course_des" class="form-control" id="course_des" rows="3"></textarea>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3 align-self-center">Featured Image:</label>
<div class="col-sm-9">
<input type="file" id="myfile" name="myfile"> </div>
</div>
<div class="form-group">
<!-- <input type="submit" name="addcourse" class="btn btn-primary" Value="addcourse">
--> <button type="submit" name="addcourse" class="btn btn-primary">Submit</button>
<button type="submit" class="btn bg-danger">Cancel</button>
</div>
</form>
Attached image is how the database looks like after submission
I have checked the file naming and i expect the generated temp file name to be stored in passport field of table, then moved it the target folder.
My basic example (upload and read a file) is like that :
<?php
if (isset($_FILES['the_key']['error']) && $_FILES['the_key']['error'] == UPLOAD_ERR_OK)
{
if (is_uploaded_file($_FILES['the_key']['tmp_name']))
{
$extension = strrpos($_FILES['the_key']['name'], '.'); // locate the last dot
if ($extension !== false)
{
$extension = substr($_FILES['the_key']['name'], 1 + $extension);
// if needed add here a custom extension validation instead of ctype_alnum
$extension = ctype_alnum($extension) ? '.' . strtolower($extension) : '.unknown';
}
else
$extension = '.no-extension';
$filename = sha1(mt_rand()) . $extension ; // get a name
move_uploaded_file($_FILES['the_key']['tmp_name'], $filename); // take the file
readfile($filename); // read
unlink($filename); // delete
exit;
}
}
?>
<form method=post enctype="multipart/form-data">
<input type=file name=the_key>
<input type=submit>

Laravel upload image field in Dashboard Error: SyntaxError: Unexpected token < in JSON at position 0

I'm running the latest version of laravel using custom admin panel and i'm new to laravel development so this is kind of a newbie question, i'm trying to upload image in a form all other fields in the ads form is working fine except the image upload field it's giving me syntax Error even though i used the same code for posts form and it's working just fine
the form upload the image to upload folder then store it's id and url in database->media table and then put the same id in the ->ads table in the images column
here is the source code for the html form
<div class="col-sm-12">
<label>Name - English</label>
<div class="form-group">
<input type="text" name="name_en" value="" class="form-control">
<i class="form-group__bar"></i>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Description - English</label>
<textarea class="wysiwyg-editor" name="description_en"></textarea>
</div>
</div>
<div class="col-sm-12 margin-bottom-30">
<label>Media - English</label>
<div class="row lightbox photos margin-bottom-30 media_sort" data-link="https://****/admin/ads/6/mediasort" data-language="en">
</div>
<div class="form-group color-picker">
<input type="file" class="form-control " name="media_en[]" accept=".jpg,.png,.jpeg,.webm,.mp4" multiple>
<i class="form-group__bar"></i>
</div>
</div>
<div class="col-sm-12">
<label>Name - Spanish</label>
<div class="form-group">
<input type="text" name="name_sp" value="" class="form-control">
<i class="form-group__bar"></i>
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<label>Description - Spanish</label>
<textarea class="wysiwyg-editor" name="description_sp"></textarea>
</div>
</div>
<div class="col-sm-12 margin-bottom-30">
<label>Media - Spanish</label>
<div class="row lightbox photos margin-bottom-30 media_sort" data-link="https://*****/admin/ads/6/mediasort" data-language="sp">
</div>
<div class="form-group color-picker">
<input type="file" class="form-control " name="media_sp[]" accept=".jpg,.png,.jpeg,.webm,.mp4" multiple>
<i class="form-group__bar"></i>
</div>
</div>
Here is the adservice.php starting from line 81
foreach ($active_languages as $language) {
$ad->translate($language->code)->name = $request['name_' . $language->code] ? $request['name_' . $language->code] : $request['name_en'];
$ad->translate($language->code)->description = $request['description_' . $language->code];
$ad->translate($language->code)->excerpt = $request['excerpt_' . $language->code];
$media_key = null;
if ($request->hasFile('media_' . $language->code)) {
$media_key = 'media_' . $language->code;
}
if ($media_key) {
$media = $this->mediaService->upload($request, $media_key);
if (count($media)) {
$media_array = [];
foreach ($media as $m) {
$m->authorable_type = \get_class(Auth::user());
$m->authorable_id = Auth::user()->id;
$m->save();
$media_array[] = $m->id;
}
$old_images = (array) (\json_decode($ad->translate($language->code)->images, true));
$media_array = \array_merge($old_images, $media_array);
$ad->translate($language->code)->images = \json_encode($media_array);
}
} else {
if (!empty($request['images_' . $language->code])) {
$media_key = ('images_' . $language->code);
}
if ($media_key) {
$ad->translate($language->code)->images = \json_encode(explode(',', $request[$media_key]));
}
}
}

Move uploaded file in php work only sometimes when use multiple uploads

I have a web page for uploading tile image and a concept design image using that tile with a single submit button. But when uploading 2 images with a single submit, move uploaded file is not working always. Sometimes it just moves tile images only, not concept image.
Here is my code:
if(isset($_POST['upload']))
{
$name=$_POST['name'];
$size=$_POST['size'];
$finish=$_POST['finish'];
/* Concept Image */
$concept=$_FILES['concept']['name'];
$contmp=$_FILES['concept']['tmp_name'];
$location='concept';
$upload=move_uploaded_file($contmp,'concept/'.$concept);
$confile='concept/'.$concept;
/* Tile Image */
$image=$_FILES['image']['name'];
$imgtmp=$_FILES['image']['tmp_name'];
$location='tileimage';
$uploading=move_uploaded_file($imgtmp,"tileimage/".$image);
$upfile="tileimage/".$image;
$qry="insert into tile_list value('','$name','$size','$finish','$upfile','$confile')";
$ex=mysqli_query($con,$qry);
$query="insert into availcolors value('','$name','$name','$upfile','$confile')";
$exe=mysqli_query($con,$query);
}
and here is my html markup:
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label class="control-label col-sm-4">TILE IMAGE<br>
</label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name=image>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">CONCEPT 3D<br>
</label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="concept">
</div>
</div>
<div class="col-sm-10 col-md-7 col-md-offset-4">
<button type="submit" name="upload"><img src="images/upload.jpg" alt="" width="106" height="25" class="img-responsive"></button>
</div>
<form>
Please help me to find out what is the problem
Please try this code:
<?php
//error_reporting(0);
if(isset($_POST['upload']))
{
$name = $_POST['name'];
$size = $_POST['size'];
$finish = $_POST['finish'];
/* Concept Image */
$aMyUploads = array();
foreach ($_FILES as $key => $aFile) {
for($i = 0; $i<count($aFile['error']); $i++){
//echo $aFile['error'][$i]; exit;
if(0 === $aFile['error'][$i]){
if($i == 0)
$newLocation = 'concept/'.$aFile['name'][$i];
else if($i == 1)
$newLocation = 'tileimage/'.$aFile['name'][$i];
}
if(0 === $aFile['error'][$i] && (false !== move_uploaded_file($aFile['tmp_name'][$i], $newLocation))){
$aMyUploads[] = $newLocation;
} else {
$aMyUploads[] = '';
}
}
}
if(is_array($aMyUploads)){
$confile=$aMyUploads[0];
$upfile=$aMyUploads[1];
$qry="insert into tile_list value('','$name','$size','$finish','$upfile','$confile')";
$ex=mysqli_query($con,$qry);
$query="insert into availcolors value('','$name','$name','$upfile','$confile')";
$exe=mysqli_query($con,$query);
}else{
echo "ERROR :: Not insert Please try";
}
}
?>
<html>
<form method="post" enctype="multipart/form-data" class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="form-group">
<label class="control-label col-sm-4">TILE IMAGE<br></label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="upload_files[]">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">CONCEPT 3D<br></label>
<div class="col-sm-10 col-md-offset-0 col-md-4">
<input type="file" class="form-control" name="upload_files[]">
</div>
</div>
<div class="col-sm-10 col-md-7 col-md-offset-4">
<button type="submit" name="upload"><img src="images/upload.jpg" alt="" width="106" height="25" class="img-responsive"></button>
</div>
</form>
</html>

Multiple File Upload results in foreach() error

Actually trying to upload multiple pictures in a folder at once with some kind of renaming. The upload goes to the foreach loop. However, it does stop at this point:
foreach ($_FILES['pictures']['name'] as $f => $name) {
if ($_FILES['pictures']['error'][$f] == 0) {
$name = $date;
$name .= "-";
$name .= $i;
$name .= ".";
$extsearch = $_FILES['pictures']['name'][$f];
$ext = pathinfo($extsearch, PATHINFO_EXTENSION);
$name .= $ext;
if(move_uploaded_file($_FILES["pictures"]["tmp_name"][$f], $path.$name)) {
if ($i == 1) {
create_image_thumb($albumFolder, $name);
}
$i++;
resize_image($albumFolder, $name);
}
}
}
However, I receive this error in the error_log and the files aren't moved:
PHP Warning: Invalid argument supplied for foreach() in /home/clublabo/public_html/management/create_album_action.php on line 43
UPDATE: Here is the HTML Code:
<form class="form form-horizontal" action="create_album_action.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="email-2" class="col-sm-3 col-md-4 control-label">Date</label>
<div class="col-sm-6 col-md-4">
<div class="input-with-icon">
<input name="dateAlbum" class="form-control" type="text" value="<?php echo date('Y-m-d'); ?>" data-provide="datepicker" data-date-autoclose="true" data-date-format="yyyy-mm-dd"><span class="icon icon-calendar input-icon"></span></div>
</div>
</div>
<div class="form-group">
<label for="password-2" class="col-sm-3 col-md-4 control-label">Pictures</label>
<div class="col-sm-6 col-md-4">
<input name="pictures" id="password-2" class="form-control" type="file" multiple>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6 col-md-offset-4 col-md-4">
<button class="btn btn-primary pull-right" type="submit">Create Album</button>
</div>
</div>
</form>
Maybe you guys has any Idea how to have this resolved?
Thanks :)
Replace:
<input name="pictures" id="password-2" class="form-control" type="file" multiple>
with:
<input name="pictures[]" id="password-2" class="form-control" type="file" multiple>
Just needed to add [] at the end of the form input "name".

Unable to save multiple images in database Laravel 5

I have a table named "cases" where the column are id image1,content1,image2,content2,image3 and content3
My content1,content2,content3 will be saved in database when i submit it from form but images are nither saved in folder nor in database. I need help
Controller
public function store(Request $request,SidContainRequest $request)
{
$sidContain = new sidcontain;
$sidContain->content1 = $request->get('page_content1');
$sidContain->content2 = $request->get('page_content2');
$sidContain->content3 = $request->get('page_content3');
//upload multiple files
$files= [];
if($request->file('leftimage')) $files[] = $request->file('leftimage');
if($request->file('Middleimage')) $files[] = $request->fiel('Middleimage');
if($request->file('Rightimage')) $files[] = $request->file('Rightimage');
foreach($files as $flle)
{
if(!empty($file))
{
$filename[] = $file->getClientOrginalName();
if(isset($filename)){
$file->move(base_path().'/frontend/sidimage/',end($filename));
}
}
$sidContain->image1 = $filename[0];
$sidContain->image2 = $filename[1];
$sidContain->image3 = $filename[2];
}
$ok = $sidContain->save();
if($ok)
{
\Session::flash('flash_message','Report Added Successfully!');
return Redirect::to('administrator/sidConatin/create');
}
}
view/form
<div class="form-group">
<label>Left Image</label>
<input type="file" name="leftimage">
#if($errors->has('leftimage')) <p style="color:red;">{{$errors->first('leftimage')}}</p>#endif
</div>
<div class="form-group">
<label>Left Content</label>
<textarea name="page_content1" id="page_content1" class="editor form-control"></textarea>
#if($errors->has('page_content1'))<p style="color:red;">{{$errors->first('page_content1')}}</p>#endif
</div>
<div class="form-group">
<label>Middle Image</label>
<input type="file" name="Middleimage">
#if($errors->has('Middleimage')) <p style="color:red;">{{$errors->first('Middleimage')}}</p>#endif
</div>
<div class="form-group">
<label>Middle Content</label>
<textarea name="page_content2" id="page_content2" class="editor form-control"></textarea>
#if($errors->has('page_content2')) <p style="color:red;">{{$errors->first('page_content2')}}</p>#endif
</div>
<div class="form-group">
<label>Right Image</label>
<input type="file" name="Rightimage">
#if($errors->has('Rightimage')) <p style="color:red;">{{$errors->first('Rightimage')}}</p>#endif
</div>
<div class="form-group">
<label>Right Content</label>
<textarea name="page_content3" id="page_content3" class="editor form-control"></textarea>
#if($errors->has('page_content3')) <p style="color:red;">{{$errors->first('page_content3')}}</p>#endif
</div>
<div style="margin-top: 15px;">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</form>

Categories