PHP file upload on registration - php

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

Related

What is required to work PHP post form method?

I am new to php so help me to make it work. Required action completed by opening the ready.php file but entered form data is not displaying in ready.php file.
i AM USING THIS CODE in index.php for form.
<form method="post" action="ready.php">
<div class="enter-name">
<input class="animated pulse infinite" type="name" required="" maxlength="50" name="n" placeholder="👉 Enter Your Name Here">
<button class="btn animated shake infinite" type="submit"><span>👉</span> Go</button>
</div>
code for ready.php
<figure>
<h1 class="naming"></h1>
<h1 class="naming"></h1>
<h1 class="naming"></h1>
<h1 class="naming"></h1>
<h1 class="naming"></h1>
or
<div class="busi"><br><img src="4.png" height="35px" width="35px"/>
I am not getting what the problem with that.
You don't need a database connection for this, but you do need to be running/have access to some sort of server with php functionality (apache is a good choice).
Regarding the code, I'm no expert, but you could try adding the "name" attribute, because that's what the php script is going to look for. I added it below (name = "name"). You also need to change the input type to text (type="text"):
<input class="animated pulse infinite" type="text" name="name" required="" maxlength="50" name="n" placeholder="👉 Enter Your Name Here">
in your php script, you can find the name variable from the textbox by using this code:
$myName = $_POST['name'];
The name will be inside the variable $myName.
Good luck!
i resolve my above problem by applying this code
<?php echo $_REQUEST['n'];?>

Undefined index when data with file path updating?

I have a file upload form it works perfect when new file upload.But index error when update. At update data, i want that if new file select it will upload new file and update the data with that file path(which worked perfect) but when updating (just want to update old data) it gives index error. i am using codeigniter.
html form :
<section>
<label class="label" for="FileUpload">
Activity Image File</label>
<label for="file" class="input input-file">
<div class="button">
<input id="fileToUploadactivity" name="fileToUploadactivity" type="file" onchange="updateInput(this.value)" > <!-- onchange is use to put the file name on below input field -->
Browse
</div>
<input type="text" name="SelectedFileName" id="SelectedFileName" value="<?php echo $value['file_path']; ?>" readonly>
in php
//if (is_uploaded_file($_FILES['fileToUploadactivity']['tmp_name'])){ // also error in here
$newfile = $_FILES["fileToUploadactivity"]["tmp_name"];
if (!empty($newfile)) {
// new file uploding code
}else{
// old data upload
}
error
Message: Undefined index: fileToUploadactivity

Uploading Files in php not working

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.

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

Upload script with output url insert into textbox to submit?

Sorry for my bad english.
I want a script, which have a image uploader, limit 2mb and one file per use, supported extensions: png, jpg, jpeg, gif. Size 468*70 only. And I need an upload button next to the text box which pastes an image link, and after the upload, it paste the image link to that text box. If user doesn't want to upload, he still can paste link on that and submit, i got my submit button, you can make your, I will recode that.
Image for eg: http://i.stack.imgur.com/rzcxv.png
if(empty($_POST) == false) {
$_POST['ip'] = htmlspecialchars($_POST['ip'], ENT_QUOTES);
$_POST['port'] = (int)$_POST['port'];
$banner = htmlspecialchars($_POST['banner'], ENT_QUOTES);
if($banner !== ''){
$imageProp = #getimagesize($banner);
if($imageProp == false){
$errors[] = "Banner link is not an image!";
}
if($imageProp[0] !== 468 || $imageProp[1] !== 60){
$errors[] = "Banner is too large or too small, only 468 * 60 accepted. Your image has: " .$imageProp[0]." * ".$imageProp[1];
}
}
<div class="form-group">
<label>Banner (468 * 60) </label>
<input class="form-control" type="text" name="banner" class="span4" /><br />
</div>
There are a lot of PHP tutorials which can help you handle the backend workflow of the file upload. You could probably handle the filetype validation and filesize validation on the frontend.
Here's some Twitter Bootstrap for the frontend presentation of your form.
<form class="form-inline" role="form" action="submit.php">
<div class="form-group">
<input type="file" id="img">
</div>
<div class="form-group">
<input type="url" class="form-control" id="url" placeholder="Or enter a URL...">
</div>
<button type="submit" class="btn btn-default">Upload</button>
</form>
Learn more about Bootstrap's inline forms
After the user submits the form you can display the link to the image in a modal.
Learn more about Bootstrap's modals

Categories