I've the following code to upload a temp file within the server.
I would like to check the file size. If the file is larger than 25MB I would like to abort the proces. If the file size is less than 25MB I would like to proceed with the upload.
$tmp_name = $_FILES['filename']['tmp_name'];
$type = $_FILES['filename']['type'];
$file_name = $_FILES['filename']['name'];
$size = $_FILES['filename']['size'];
Anyone a suggestion?
Tnx for any reply.
You could use something such as:
if($_FILES['file']['size'] > 26214400) {
echo "File too large. Uploaded files can be no more than 25MB.";
} else {
... let them upload ...
Related
I am trying to import data from a .CSV file into SQL Server automatically. For example, if I have a set of .CSV files in a folder, I want the data from those .CSV files to be imported into SQL Server automatically, every time I add a new .CSV file to that folder.
If you want it to be truly automatic, you will need to go beyond SQL. Otherwise, scheduling a load from the csv to your database would require a simple SSIS package. Then you can go and schedule a job using SQL Server Agent to import the data daily, weekly, hourly, etc.
EDITED!! THIS IS FOR PHP
$filenames = glob($_SERVER['DOCUMENT_ROOT'] .'/filespath/*');
foreach($filenames as $filename){ // iterate files
if(!file_exists($filename) || substr($filename,-3) == 'csv') continue;
$file = fopen($filename, "r");
$csv = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
$csv[] = $line;
}
fclose($file);
foreach ($csv as $key => $value) {
//put your code here to work with $csv array.
}
}
You can use this code to get what you want. You also need cron to automate your code. You can get it in your host cpanel.
I have upload CSV file but problem is i don't know how to read data of uploaded csv file in Magento2.
Please help me anyone.
Thanks in advance.
You can do that like you could in Magento 1. In Magento 1 you would use
new Varien_File_Csvbut in Magento 2 you can do the same with \Magento\Framework\File\Csv. You can use the following code.
In your __construct() inject the following classes:
protected $_fileCsv;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Module\Dir\Reader $moduleReader,
\Magento\Framework\File\Csv $fileCsv
) {
$this->_moduleReader = $moduleReader;
$this->_fileCsv = $fileCsv;
parent::__construct($context); // If your class doesn't have a parent, you don't need to do this, of course.
}
Then you can use it like this:
// This is the directory where you put your CSV file.
$directory = $this->_moduleReader->getModuleDir('etc', 'Vendor_Modulename');
// This is your CSV file.
$file = $directory . '/your_csv_file_name.csv';
if (file_exists($file)) {
$data = $this->_fileCsv->getData($file);
// This skips the first line of your csv file, since it will probably be a heading. Set $i = 0 to not skip the first line.
for($i=1; $i<count($data); $i++) {
var_dump($data[$i]); // $data[$i] is an array with your csv columns as values.
}
}
I'm new to CodeIgniter and having the following issue. When I upload a file, its successfully uploaded on my local folder and the file name saved to the db. The problem is, file name has been changed after uploading.
eg:
file name "screenshot image 123.png" -> "screenshot_image_123.png"
It just convert the space into underscore;this issue occurred only when the file name having space.Other than this issue all the other files uploading successfully. Given below is my code which I used on my controller page:
public function upload_pic()
{
$image_path = realpath(APPPATH . '../uploads');
$config['upload_path'] = $image_path;
$config['allowed_types'] = "gif|jpg|jpeg|png";
$config['file_name'] = $_FILES['file']['name'];
$config['encrypt_name'] = TRUE;
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('file'))
{
echo $this->upload->display_errors();
}
else
{
$finfo=$this->upload->data();
$data['uploadInfo'] = $finfo;
}
}
Can anyone help me????
Try before saving file name generate some unique
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$filename = sha1_file($filename). md5(date('Y-m-d H:i:s:u')) . '.'. $ext; //not only this you can generate any format
$config['file_name'] = $filename; //Use this file name is db too
I need to make possible for people to send their documents, be it .docx, .pdf or whatever from their computers, using PHPMailer. Of every solution I found, none of them worked for me. The error Could not access file: keeps showing up when using $mailer->ErrorInfo.
This is the code I have:
$mailer->From = $mail1;
$mailer->FromName = $name1;
$mailer->addAddress("gmfernandes#neo-e.com.br");
$mailer->Subject = $name1;
$mailer->ContentType = "Content-type: text/html; charset=utf-8";
$mailer->msgHTML($template);
$mailer->addAttachment($_FILES['anexoTrabalho']['tmp_name'], $_FILES['anexoTrabalho']['name']);
Thank you in advance
You need to learn how to handle uploads correctly. Don't access $_FILES directly; use move_uploaded_file first. To save you the hassle of looking it all up, adapt the example provided with PHPMailer, the important bit of which I reproduce here:
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
// First handle the upload
// Don't trust provided filename - same goes for MIME types
// See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
// Upload handled successfully
// Now create a message
// This should be somewhere in your include_path
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->setFrom('from#example.com', 'First Last');
$mail->addAddress('whoto#example.com', 'John Doe');
$mail->Subject = 'PHPMailer file sender';
$mail->msgHTML("My message body");
// Attach the uploaded file
$mail->addAttachment($uploadfile, 'My uploaded file');
if (!$mail->send()) {
$msg = "Mailer Error: " . $mail->ErrorInfo;
} else {
$msg = "Message sent!";
}
} else {
$msg = 'Failed to move file to ' . $uploadfile;
}
}
I want to upload a file inside the sites/default/files/myuploads folder. But I am not able to. Actually I am not able to upload the file to any of the folder inside sites/default/files/myuploads.
Here is my code:
$validators = array('file_validate_extensions' => array('se', 'SE'));
$file = file_save_upload('fname',$validators, 'public://myuploads', FILE_EXISTS_REPLACE);
if ($file) {
$fid = $file->fid;
$doc_url = file_create_url($file->uri);
//Set the status of the uploaded file.
$file->status = FILE_STATUS_PERMANENT;
//other code...
}
The upload will work fine if I just give public://. So it will be in the sites/default/files folder. And when I print the $file, in that the ["destination"] => "sites/default/files/./test.se"
How can I do this? Any idea how I can upload into the myuploads folder ?
Verify the write permission for "myuploads" folders.
Change the $validators variable like this:
$validators = array('file_validate_extensions' => array('se SE'));