I have a custom module by which i am trying to upload images. However i cant upload images
This is my form file
<?php
<form id="imageform" method="post" action="<?php echo $this->getFormAction(); ?>">
<input type="file" id="chain_Image" value="" name="Image[]" class="input-text">
</form>
note that this is only a small part of my code. actually this form can contain any number images. that is we can upload more than one image at a time.
This is my saveAction() :
<?php
$arr = $this->getRequest()->getPost('Image');
foreach($arr as $image)
{
if(isset($_FILES[$image]['name']) and (file_exists($_FILES[$image]['tmp_name'])))
{
try
{
$path = Mage::getBaseDir('media') . DS . 'banner' . DS;
$uploader = new Varien_File_Uploader($image);
$uploader->setAllowedExtensions(array('jpg','png','gif','jpeg'));
$uploader->setAllowRenameFiles(false);
$uploader->setFilesDispersion(false);
$destFile = $path.$_FILES[$image]['name'];
filename = $uploader->getNewFileName($destFile);
$uploader->save($path, $filename);
$postData['Image'] = $_FILES[$image]['name'];
}
catch(Exception $e)
{}
}
else
{
if(isset($postData[$image]['delete']) && $postData[$image]['delete'] == 1)
$postData[$image] = '';
else
unset($postData[$image]);
}
}
$bannerModel->addData($postData)->setId($this
->getRequest()->getParam('id'))->save();
However it does not upload my imaged. $_FILES is seem to be empty,when i have printed this. What is the problem in my code. Help me. Thanks in advance !!!
I hope there is a mistake at save function. Here is my example code for saving an image with other details. Let see this may help you.
$newsModel->setId($this->getRequest()->getParam('id'))
->setTitle($postData['title'])
->setImage($_FILES['image']['name'])
->setContent($postData['content'])
->setStatus($postData['status'])
->save();
You have to save the image by calling as setImage($_FILES['image']['name']) Try this..
Remember to put the enctype="multipart/form-data" in form parameters :
<form id="imageform" method="post" action="<?php echo $this->getFormAction(); ?>" enctype="multipart/form-data">
<input type="file" id="chain_Image" value="" name="Image[]" class="input-text">
</form>
Related
I want to save a pdf file in the storage folder but when I insert a file in my form and I click on the button, it displays me the error "Path cannot be empty". However, the path is not empty.
Here is my function that allows me to do this :
public function getFilenametostore(Request $request): string
{
$filenamewithextension = $request->file('profile_pdf')->getClientOriginalName();
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('profile_pdf')->getClientOriginalExtension();
$time = time();
//filename to store
$filenametostore = $filename . '_' . $time . '.' . $extension;
//The problem is here
$request->file('profile_pdf')->storeAs('profile_pdfs', $filenametostore);
return $filenametostore;
}
in storage, there is: storage/app/public/profile_pdfs/
My Form :
<form action="{{url('poste')}}" method="post" enctype="multipart/form-data">
#csrf
<div class="mt-2">
<label class=" block text-sm text-gray-600" for="cus_email">Upload</label>
<input class="px-2 py-2 text-gray-700 rounded" type="file" name="profile_pdf" id="exampleInputFile">
</div>
<div class="mt-4">
<button class="px-4 py-1 text-white font-light tracking-wider bg-gray-900 rounded" type="submit">Publier</button>
</div>
</form>
My controller :
if($files = $request->hasFile('profile_pdf'))
{
$filenametostore = $this->getFilenametostore($request);
$p = new Poste;
$p->pdf = $filenametostore;
$p->save();
return redirect()->route('poste.index');
}
Go to php.ini file uncomment upload_tmp_dir and set the value c:\windows\temp
;upload_tmp_dir =
Replace like this
upload_tmp_dir = c:\windows\temp
if you use windows
I just solved this problem. If your website is hosted on windows server go to your temp folder (C:\Windows\Temp) and right click -> properties -> edit your user's permissions to full control like this.
after that if you get an alert just click ok.
Don't forget to login with your administrator user to able to edit permissions.
You can try one of these method
$fileName = $file.'.'.$ext;
$request->file->storeAs($path, $fileName);
// or
Storage::disk('local')->put($path . '/' . $fileName , $request->file);
I need to upload file from dynamic upload button that generated form array as below code (Edited and workd already).
#foreach( $transfer as $key => $item )
<tr>
<td>
<input type="file" name="document[]" class="doc filestyle"/>
</td>
</tr>
#endforeach
This is boostrap filestyle
<script type="text/javascript">
$('.doc').filestyle({
buttonName : 'btn-success',
input: false,
icon: false,
});
It only work for single file. But I have no idea for multiple. (Edited and workd already)
if(count(Request::file('document', [])) > 0){
foreach( Request::file('document', []) as $key => $item ){
echo 'Reg ID: ' . $key . '<br />';
echo 'Value: ' . $item->getClientOriginalName() . '<br />';
}
}
Thanks for all advise.
Thats because you are not defining your control name as array
Change This to
<input type="file" name="document" id="doc{{$key}}" class="filestyle"/>
This
<input type="file" name="document[]" id="doc{{$key}}" class="filestyle"/>
Not name="document" change to name="document[]"
and sorry i have missed multiple Attributes in tag thanks to #Md.Sukel Ali
This Might Work
You need to add multiple attribute in your in input field.
<input type="file" name="document[]" id="doc{{$key}}" class="filestyle" multiple="" />
I would like to create a direct unsubscribe page in magento, I found this instruction to follow but the steps 1 and 2 are not clear As I'm not a professional.
Can someone please help me clarify these two steps. Where to create the "unsubscribe.phtml" page? How to add the just created block in it?
Thank you in advance.
1. Create a phtml page say “unsubscribe.phtml” containing the code to create the unsubscribe form.
<?php $newsletterObj = new Mage_Newsletter_Block_Subscribe(); ?>
<div class="newsletter-unsubscribe">
<div class="newsletter-unsubscribe-title"><?php echo $this->__('Submit your email id to unsubscribe newsletter') ?></div>
<form action="<?php echo $newsletterObj->getUnsubscribeFormActionUrl() ?>” method="post" id="newsletter-validate-detail">
<div class="block-content">
<div class="input-box">
<input type="text" name="email" id="newsletter" title="<?php echo $this->__('Sign up for our newsletter') ?>” class="input-text required-entry validate-email” value="<?php echo $this->__('Enter Your Email Here') ?>” onfocus="if(this.value==’<?php echo $this->__('Enter Your Email Here') ?>’)this.value=’’;” onblur="if(this.value==’’)this.value=’<?php echo $this->__('Enter Your Email Here') ?>’;”
/>
</div>
<div class="actions">
<button type="submit" title="<?php echo $this->__('Submit') ?>” class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
</div>
</div>
</form>
<script type="text/javascript\">
//<![CDATA[
var newsletterSubscriberFormDetail = new VarienForm(’newsletter-validate-detail’);
//]]>
</script>
</div>
2) Create a CMS page. Add the just created block in it. So that your CMS page will contain that form.
3) Now in page \app\design\frontend\base\default\template\newsletter\subscribe.phtml add the code to add a link of the cms page.
<div class="unsubscribe">
<?php echo $this->__('Unsubscribe') ?>
</div>
4) In page \app\code\core\Mage\Newsletter\Block\Subscribe.php add a function to create the form action url which is called in the “unsubscribe.phtml”.
public function getUnsubscribeFormActionUrl()
{
return $this->getUrl(’newsletter/subscriber/unsubscribecus’, array(’_secure’ => true));
}
5) Now in \app\code\core\Mage\Newsletter\controllers\SubscriberController.php page add new action for unsubscribe process.
/**
* Unsubscribe newsletter from frontend
*/
public function unsubscribecusAction()
{
$email = $this->getRequest()->getParam(’email’);
$subsModel = Mage::getModel(’newsletter/subscriber’);
$subscriber = $subsModel->loadByEmail($email);
$id = (int) $subsModel->getId();
$code = (string) $subsModel->getCode();
if ($id && $code) {
$session = Mage::getSingleton(’core/session’);
try {
Mage::getModel(’newsletter/subscriber’)->load($id)
->setCheckCode($code)
->unsubscribe();
$session->addSuccess($this->__(’You have been unsubscribed.’));
}
catch (Mage_Core_Exception $e) {
$session->addException($e, $e->getMessage());
}
catch (Exception $e) {
$session->addException($e, $this->__(’There was a problem with the un-subscription.’));
}
}
$this->_redirectReferer();
}
Since a can't leave a comment and this question isn't marked as solved yet, i'll assume you still need an answer.
I would suggest placing the unsubscribe.phtml file in /template/newsletter/
For step 2 you can use this code
{{block type="core/template" template="newsletter/unsubscribe.phtml"}}
so the page will contain your form.
If you already figured out how to do this, please post an answer to your own question further on.
Would it be an idea to add an unsubscribe button next to the subscribe button (or allow for a variable in the block call that sets it to yes/no display) - this way you capture both
I want to know how I can upload an image and save its name in a database when using Codeigniter.
I am using a simple form tag for this, like:
<form action="./myaccount/index/" method="post" enctype="multipart/form-data">
<div>
<label for="pic">picture</label>
<input type="file" id="pic" name="pic" />
</div>
<input id="register-btn" name="register" type="submit" value="ok" class="input-text custom-btn">
</form>
Here's my controller:
public function index() {
$user = new User($_SESSION['user_id']);
if($this->input->post()){
$user->pic = $this->input->post('pic');
if($this->input->post('password') == $this->input->post('confirm')){
$user->password=md5(sha1(sha1($this->input->post('password'))));
$user->save();
$this->data['success'] = "done";
}else{
$this->errors[] = "error";
}
}
$this->data['user'] = $user;
$this->data['errors'] = $this->errors;
$this->template->set_layout('myaccount');
$this->template->build('profile',$this->data);
}
I checked the manual but I can't understand what they are doing.
Im' trying to make a controler function for this that will insert all the values in the database and also upload the image file.
I am trying to embed the wordpress registration into the homepage. I already created a custom register page, the problem is that it is calling to the header information and I get this error, when I include it with php include.
Warning: Cannot modify header information - headers already sent
Is there a way I can use my code but alter it somehow/work with it somehow, so that the registration form is fully embedded/works in the page using ajax, but that it works. I am not brilliant with Php so please excuse if this is a silly question:
Current register code(works in its own page but not when included in the homepage template)
<?php
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
//Check whether the user is already logged in
if (!$user_ID) {
if($_POST){
//We shall SQL escape all inputs
$username = $wpdb->escape($_POST['username']);
if(empty($username)) {
echo "User name should not be empty.";
exit();
}
$email = $wpdb->escape($_POST['email']);
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
echo "Please enter a valid email.";
exit();
}
$random_password = wp_generate_password( 12, false );
$status = wp_create_user( $username, $random_password, $email );
if ( is_wp_error($status) )
echo "Username already exists. Please try another one.";
else {
$from = get_option('admin_email');
$headers = 'From: '.$from . "\r\n";
$subject = "Registration successful";
$msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
wp_mail( $email, $subject, $msg, $headers );
echo "Please check your email for login details.";
}
exit();
} else {
echo "";
?>
<!-- <script src="http://code.jquery.com/jquery-1.4.4.js"></script> --> <!-- Remove the comments if you are not using jQuery already in your theme -->
<?php
if(get_option('users_can_register')) { //Check whether user registration is enabled by the administrator
?>
<h1><?php the_title(); ?></h1>
<br /><br />
<div id="result"></div> <!-- To hold validation results -->
<form id="wp_signup_form" action="" method="post">
<label><p>Username:</p></label>
<input type="text" name="username" class="text" value="" />
<br /><br />
<label><p>Email address:</p></label>
<input type="text" name="email" class="text" value="" /> <br />
<br />
<input type="submit" id="submitbtn" class="Buttons" name="submit" value="Register" />
<br />
<br />
</form>
<script type="text/javascript">
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url: "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
data: input_data,
success: function(msg){
$('.loader').remove();
$('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;
});
</script>
<?php
}
else echo "Registration is currently disabled. Please try again later.";
?>
</div>
</div>
<?php
echo "";
} //end of if($_post)
}
else {
wp_redirect( home_url() ); exit;
}
?>
is not safe to use a custom method for registration and mailing.
so i suggest to look at this http://digwp.com/2010/12/login-register-password-code/
i hope that's what you want.
best regard.
As webfan suggests, it isn't a good idea to reproduce the registration functionality on the homepage. Much better would be to simply cut and paste the login form from the registration page and keep the form action pointed to the registration page:
<form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>">
<!-- registration form HTML goes here -->
</form>