Uploading Files in php not working - php

Can anyone help me solve this problem of file upload in php. I am getting undefined index at line! The codes are in two seperate files. I tried several codes, still not working. Please help. Thank you all in advance.
HTML File
<form role="form" name="uploadPro" id="uploadPro" method="post" action="upload.php" enctype="multipart/form-data">
<div class="col-lg-6">
<div class="form-group">
<label>Product Tags</label>
<textarea class="form-control" rows="2" name="pTags" id="PTags"></textarea>
<p class="help-block">Add Tags to your products to enable faster search.</p>
</div>
<div class="form-group">
<label>Product Expiry Date</label>
<input type="text" name="pExpDate" id="datepicker" class="form-control">
<p class="help-block">Until which date will product be displayed.</p>
</div>
<div class="form-group">
<label>Upload Main Image</label>
<input type="file" name="mainPath" id="mainPath">
<p class="help-block">This image will appear everywhere. Make sure it has a good quality to impress users and size greater than 400px X 400px. </p>
</div>
<div class="form-group">
<label>Upload Sub Image 1</label>
<input type="file" name="sub1" id="sub1">
<p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p>
</div>
<div class="form-group">
<label>Upload Sub Image 2</label>
<input type="file" name="sub2" id="sub2">
<p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p>
</div>
<div class="form-group">
<label>Upload Sub Image 3</label>
<input type="file" name="sub3" id="sub3">
<p class="help-block">Make sure image has a good quality to impress users and size greater than 400px X 400px. </p>
</div>
<input type="submit" value="Upload Image" name="submit" class="btn btn-default">
</div>
</form>
PHP Code
if(isset($_POST['submit'])){
/*Uploading main image*/
$filetmp = $_FILES["mainPath"]["tmp_name"];//getting error here
$filename = $_FILES["mainPath"]["name"];//getting error here
$filepath = "../uploads/".$filename;
move_uploaded_file($filetmp, $filepath);
/*Uploading sub image1*/
$filetmp1 = $_FILES["sub1"]["tmp_name"];//getting error here
$filename1 = $_FILES["sub1"]["name"];//getting error here
$filepath1 = "../uploads/".$filename1;
move_uploaded_file($filetmp1, $filepath1);
/*Uploading sub image2*/
$filetmp2 = $_FILES["sub2"]["tmp_name"];//getting error here
$filename2 = $_FILES["sub2"]["name"];//getting error here
$filepath2 = "../uploads/".$filename2;
move_uploaded_file($filetmp2, $filepath2);
/*Uploading sub image3*/
$filetmp3 = $_FILES["sub3"]["tmp_name"];//getting error here
$filename3 = $_FILES["sub3"]["name"];//getting error here
$filepath3 = "../uploads/".$filename3;
move_uploaded_file($filetmp3, $filepath3);
}

First, ensure that PHP is configured to allow file uploads.
In your "php.ini" file, search for the file_uploads directive, and set it to On:
file_uploads = On
Error Messages Explained
PHP returns an appropriate error code along with the file array. The error code can be found in the error segment of the file array that is created during the file upload by PHP. In other words, the error might be found in $_FILES['userfile']['error'].

First use print_r($_FILES) to check if the values r posting.
Use for loop instead of accessing index,
if(isset($_POST['submit'])){
foreach ($_FILES as $row) {
$filetmp = $row['tmp_name']; //getting error here
$filename = $row["name"]; //getting error here
$filepath = "../uploads/" . $filename;
move_uploaded_file($filetmp, $filepath);
}

I can check you code in my test file and its working file it may be issue is on your "php.ini" file. please send me your error so i can identify.

I did not change anything. I restarted my Wamp server and run the page in incognito window. Its working now.
Thank you all for your help. It makes me really happy to see how you help each other.

Related

Not able to upload more than 5 images in PHP

I'm trying to upload some images in my PHP application but I'm unable to do so. After some images, the post doesn't send data. It depends on the file size how many I can send like sometimes it works for 5 images sometimes for 3 images itself it throws this error.
<form action="store.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Top Image:</label><br>
<input type="file" name="topimg" class="form-control-file" style="padding-bottom:15px;">
</div>
<div class="form-group">
<label>Sub Images:</label><br>
<input type="file" name="img[]" class="form-control-file" id="exampleFormControlFile2" multiple required>
</div>
<center>
<button type="submit" class="btn btn-success" name="button">Save</button>
</center>
</form>
This is the form I'm using and it sends data to store.php
$uploaddir = '../../../img/gallery/';
$dirname = "/img/gallery/";
$newname = time() . basename($_FILES['topimg']['name']);
$fileup = $dirname . $newname;
$uploadfile = $uploaddir . $newname;
$img = '';
foreach ($_FILES['img']['name'] as $nam) {
$img = $img.",".$dirname.time().$nam;
}
$img = substr($img,1);
The above part is for setting the name and then the code to insert into my DataBase follows this (I think it's unnecessary so I left it out)
move_uploaded_file($_FILES['topimg']['tmp_name'], $uploadfile);
$count=0;
foreach ($_FILES['img']['name'] as $filename)
{
$tmp=$_FILES['img']['tmp_name'][$count];
$count=$count + 1;
move_uploaded_file($tmp,$uploaddir.time().$filename);
$tmp='';
}
Then this code to upload the files.
So when I try to upload the files it says "Undefined index: topimg" and "Undefined index: img" with errors that relate to these being invalid. I'm I doing the PHP part wrong or is it some setting in the server. I'm using MAMP pro if this info is needed
I think you need to change the upload_max_filesize and post_max_size directives in php.ini. See the php website https://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize for more information on these directives.
To change the php.ini file for mamp pro, check this question.

Upload limits on Heroku / Bucketeer (S3)

I am using Heroku and their S3 addon - "Bucketeer".
I have this working and I can upload images no problem. The thing is I don't care about uploading images. My app deals with uploading MP3s (nothing sinister - it's a web app for local bands).
I can't upload MP3s. It doesn't error out or anything, it's just that files don't end up in S3 - whereas images uploaded exactly the same way, do.
Editing to add - It's not a file type issue as I tested by uploading a very small mp3 file and this worked great.
Code below:
<form class="form" action="../model/process-track-upload.php" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="dashboard-widget">
<h3>Add a song to your collection</h3>
<input class="input" type="text" name="trackname" placeholder="Track Name" required/>
<input class="input-margin" name="userfile" type="file">
<input type="hidden" name="userID" value="<?php echo $userID ?>" />
<button class="btn btn-large btn-block " type="submit" name="btn-upload">upload</button>
</div>
</div>
</form>
And the backend code:
require('../vendor/autoload.php');
$s3 = Aws\S3\S3Client::factory();
$bucket = getenv('S3_BUCKET')?: die('No "S3_BUCKET" config var in found in env!');
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['userfile']) && $_FILES['userfile']['error'] == UPLOAD_ERR_OK && is_uploaded_file($_FILES['userfile']['tmp_name'])) {
try {
$upload = $s3->upload($bucket, $_FILES['userfile']['name'], fopen($_FILES['userfile']['tmp_name'], 'rb'), 'public-read');
?>
<p>Upload successful :)</p>
<?php } catch(Exception $e) { ?>
<p>Upload error :(</p>
<?php } }
The answer to this question lay in default php settings.
HOWEVER as I was using Heroku, I didn't have access to the php.ini file.
So the answer was to create a .users.ini file which is like a user created override of the php.ini.
In that file I just set the following params:
upload_max_filesize = 15M
post_max_size = 15M
max_input_time = 0
LimitRequestBody 0
After this I was good to go!

PHP file upload on registration

I have been working on a registration form for opencart for a while now. I think I am nearly there in how I have coded it.
However I just do not know where I am going wrong with it.
It is a standard form with text input fields and I just now need a file to be uploaded also on registration. I have added the upload button and added controller code just not working.
This is what I have so far:
Template file:
<legend>Documents</legend>
<p>Please upload as .pdf to ensure a smooth upload, thank you.</p>
<div class="form-group required">
<label class="col-sm-2 control-label" for="input-ast">AST:</label>
<div class="col-sm-10">
<input type="file" name="ast" value="<?php echo "AST"; ?>" placeholder="<?php echo $entry_ast; ?>" id="input-ast" class="form-control" />
</div>
</div>
Controller:
$data['ast'] = '';
$uploads_dir = 'tenant/upload/'; // set you upload path here
if (is_uploaded_file($this->request->files['ast']['tmp_name'])) {
move_uploaded_file($this->request->files['ast']['tmp_name'],$uploads_dir.$this->request->files['ast']['name']);
$data['ast'] = $this->request->files['ast']['name'];
}
Any input would be greatly appreciated

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()));

how can i automatically create thumbnail images from an uploaded file With GD library

how can i create automatically tumbling .. I need when i upload original image automatically create thumb and add thumb ( size 200 x 200 ) in folder and insert path of thumb in database $thumb .. i need help .. any one can help me ?
<?php
error_reporting(0);
if ($_POST['submit'])
{
$name=basename($_FILES['file_upload']['name']);
$t_name=$_FILES['file_upload']['tmp_name'];
$dir='upload';
$image_title=$_POST['image_title'];
$image_details=$_POST['image_details'];
$image_text=$_POST['image_text'];
$cat=$_POST['cat'];
$thumb=$_POST['thumb'];
if(move_uploaded_file($t_name,$dir."/".$name))
{
mysql_select_db ($db_name,$conn);
$qur="insert into images (mid, cid, name, path, image_title, image_details, image_text, thumb, addGby, addGon)
values('','$cat','$name','upload/$name','$image_title','$image_details','$image_text','$thumb','$session_name',now())";
$res=mysql_query($qur,$conn);
echo 'File Upload sussfully';
}
else
{
echo 'upload filed!';
}
}
?>
<form method="post" action="Addgallery.php" enctype="multipart/form-data">
<div class="field">
<label>image : </label>
<input type="file" class="custom-file-input" name="file_upload" id="file_upload" />
</div>
<div class="field fullwidth last">
<input type="submit" name="submit" value="upload" class="bt blue large" />
</div>
</form>
If you dont want to write your own code try these
http://phpthumb.sourceforge.net/
http://davidwalsh.name/create-image-thumbnail-php
http://webcheatsheet.com/php/create_thumbnail_images.php
You should try imgBrowz0r http://freecode.com/projects/imgbrowz0r
It is a php-written plugin
Css-tricks also have a tutorial on it.

Categories