I need help on how to get this code to take image files, rename them and upload separate copies. I want the original copies to be left exactly as they are.
<?php
$fileName = $_FILES["uploaded_file"]["name"];
$fileTmpLoc = $_FILES["uploaded_file"]["tmp_name"];
$fileType = $_FILES["uploaded_file"]["type"];
$fileSize = $_FILES["uploaded_file"]["size"];
$fileErrorMsg = $_FILES["uploaded_file"]["error"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
$kaboom = explode(".", $fileName);
$fileExt = end($kaboom);
$fileName = rand(1, 10).".".$fileExt;
if (!$fileTmpLoc) {
echo "ERROR: Please browse for a file before clicking the upload button.";
exit();
} else if($fileSize > 5242880) {
echo "ERROR: Your file was larger than 5 Megabytes in size.";
unlink($fileTmpLoc);
exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
echo "ERROR: Your image was not .gif, .jpg, or .png.";
unlink($fileTmpLoc);
exit();
} else if ($fileErrorMsg == 1) {
echo "ERROR: An error occured while processing the file. Try again.";
exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "uploads/$fileName");
if ($moveResult != true) {
echo "ERROR: File not uploaded. Try again.";
exit();
}
echo "The file named <strong>$fileName</strong> uploaded successfuly.<br /><br />";
echo "It is <strong>$fileSize</strong> bytes in size.<br /><br />";
echo "It is an <strong>$fileType</strong> type of file.<br /><br />";
echo "The file extension is <strong>$fileExt</strong><br /><br />";
echo "The Error Message output for this upload is: $fileErrorMsg";
?>
Can anyone help?
Related
i m learning a tutorial on fileupload in laravel.
my file is not being uploaded.
there seems to be problem at this code.
//move uploaded file
$destinationPath = 'uploads';
$file = move($destinationPath,$file->getClientOriginalName());
and where exactly i need to create a destination path
These are my full codes
uploadfile.php
<body>
<?php
echo Form::open(array('url' => '/uploadfile','files' => 'true'));
echo "Select the file to upload!";
echo Form::file('image');
echo Form::submit('Upload File');
echo Form::close();
?>
</body>
FileUploadController.php
<?php
namespace FirstProject\Http\Controllers;
use Illuminate\Http\Request;
class FileUploadController extends Controller
{
public function index(){
return view('uploadfile');
}
public function showUploadFile(Request $req){
$file = $req->file('image');
//display the fullname
echo "File Name: " .$file->getClientOriginalName();
echo "<br>";
//display file extension
echo "File Extension: " .$file->getClientOriginalExtension();
echo "<br>";
//display file realpath
echo "File Real Path: " .$file->getRealPath();
echo "<br>";
//display file size
echo "File Size: " .$file->getSize();
echo "<br>";
//display file mime type
echo "File Mime Type: " .$file->getMimeType();
//move uploaded file
$destinationPath = 'uploads';
$file = move($destinationPath,$file->getClientOriginalName());
}
}
?>
web.php
Route::get('/uploadfile','FileUploadController#index');
Route::post('/uploadfile','FileUploadController#showUploadFile');
I am using CodeIgniter to download a file created by using the following code:
header('Content-type: text/csv');
header('Content-disposition: attachment;filename=Pricemaster.csv');
echo "Category,Supplier,Org Name,Org Modelno,Capacity,Our Modelno,China,OEM + Freight,Cost Price,USD %,USD price,GBP price,INR %,INR Conversion rate,INR price" . PHP_EOL;
foreach ($data as $val) {
$imp = implode(',', $val);
echo $imp . PHP_EOL;
}
I want to save this file to my upload folder. How can I do this?
You can write files to the server by using the File Helper
Firstly load the file helper:
$this->load->helper('file');
Then try this:
$file_path = './uploads/file.php'
$output = "Category,Supplier,Org Name,Org Modelno,Capacity,Our Modelno,China,OEM + Freight,Cost Price,USD %,USD price,GBP price,INR %,INR Conversion rate,INR price" . PHP_EOL;
foreach ($data as $val) {
$imp = implode(',', $val);
$output .= $imp . PHP_EOL;
}
if ( ! write_file($file_path, $output)) {
echo 'Unable to write the file';
} else {
echo 'File written!';
}
You'll want to change the file name / path accordingly. The data will obviously be your CSV output.
If the file writes successfully you can use this path to attach to your email.
I use this code:
$usuario=getJoomUser();
$username=strtolower($usuario['username']);
$name=$usuario['name'];
if ($usuario['id']== 0)
{
echo "$name";
print "No estas logueado en el sistema";
}
else
{
The code works fine but not with some computers. Joomla load fine the user and his password but not my code in PHP. However, if I use that information in other computers works fine.
Any ideas?
Thanks a lot.
My function is:
function getJoomUser() {
error_reporting(1);
define( '_JEXEC', 1 );
define( 'DS', '/' );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] . DS . 'Joomla3.2.3' );
if(is_file(JPATH_BASE .DS.'configuration.php')) {
require_once(JPATH_BASE .DS.'configuration.php');
} else {
echo "Could not locate configuration.php <br />";
}
if(is_file(JPATH_BASE .DS.'includes/defines.php')) {
require_once(JPATH_BASE .DS.'includes'.DS.'defines.php');
} else {
echo "Could not locate defines.php <br />";
}
if(is_file(JPATH_BASE .DS.'includes/framework.php')) {
require_once(JPATH_BASE .DS.'includes'.DS.'framework.php');
} else {
echo "Could not locate framework.php<br />";
}
if(is_file(JPATH_BASE .DS.'libraries/joomla/factory.php')) {
require_once(JPATH_BASE .DS.'libraries/joomla/factory.php');
} else {
echo "Could not locate factory.php<br />";
}
$mainframe = JFactory::getApplication('site');
$user = JFactory::getUser();
$userData['username'] = $user->username;
$userData['name'] = $user->name;
$userData['id'] = $user->id;
$userData['email'] = $user->email;
$userData['groups'] = $user->groups;
return $userData;
}
?>
I have tried with the option -->guest and the problem continues. The key is: That happens in some pcs and I try to sign in with these user's data in my computer or another computer works fine.
I tried without to call a function and remains the same.
I don't know where you got getJoomUser from but to get the user object, you need to use this:
$user = JFactory::getUser();
You can then do what you wish like so:
if ($user['id'] == 0){
print "No estas logueado en el sistema";
}
else{
echo $user->username;
}
I have update to 3.3.6 and the problem continues.
On Internet Explorer, activating compatibility view works fine. WRONG
I will change to drupal...
Thanks
my Uploads folder structure in codeigniter is like
Uploads
-File1.txt
-File2.txt
-Subfolder 1
-- File1.txt
--File2.txt
-Subfilder2
--File1.txt
I want to display each file name and if that file is in subfilder it should display as Subfilder/File.txt
Thats what my code looks like
" <?php
foreach ($direcotry as $key => $value ) {
if(!is_numeric($key)){
echo $key . '/' ;
foreach ($key as $subKey => $value) {
echo $value . '<br>';
}
}
else{
echo $value . '</br>';
}
}
?>"
![enter image description here][1]
and it gives an error as invalid argument to foreach
CodeIgniter contains directory helper which provides this for you
$this->load->helper('directory');
$map = directory_map('path/to/uploads');
print_r($map)
I am using Fineuploader for an ajax upload of changing a users profile picture, I have part of my upload.php file below:
function handleUpload($uploadDirectory, $replaceOldFile = FALSE){
if (!is_writable($uploadDirectory)){
return array('error' => "Server error. Upload directory isn't writable.");
}
if (!$this->file){
return array('error' => 'No files were uploaded.');
}
$size = $this->file->getSize();
if ($size == 0) {
return array('error' => 'File is empty');
}
if ($size > $this->sizeLimit) {
return array('error' => 'File is too large');
}
$pathinfo = pathinfo($this->file->getName());
$filename = $pathinfo['filename'];
//$filename = md5(uniqid());
$ext = #$pathinfo['extension']; // hide notices if extension is empty
if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
$these = implode(', ', $this->allowedExtensions);
return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
}
$ext = ($ext == '') ? $ext : '.' . $ext;
if(!$replaceOldFile){
/// don't overwrite previous files that were uploaded
while (file_exists($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)) {
$filename .= rand(10, 99);
}
}
$this->uploadName = $filename . $ext;
if ($this->file->save($uploadDirectory . DIRECTORY_SEPARATOR . $filename . $ext)){
return array('success'=>true);
} else {
return array('error'=> 'Could not save uploaded file.' .
'The upload was cancelled, or server error encountered');
}
}
so if the image is uploaded with the same name, it creates a duplicate with a number so if asd.jpg is uploaded twice its asd44.jpg .. but I can not find how to make a variable to get the image path with random number created.
Only the initial name of the file that was uploaded. Does anyone know how to go about doing this and then making the update statement to send to database?