Localhost and fopen - codeigniter

We are now working in a local environment, rather than working live, which is a good thing. However, I've run into a hiccup I don't know how to fix. My code (CodeIgniter) looks like this:
if (!is_dir(base_url() . 'channel-partners/html/camera-registration/' . $name_url)) {
mkdir(base_url() . 'channel-partners/html/camera-registration/' . $name_url);
}
$handle = fopen('/path/to/file/channel-partners/html/camera-registration/' . $name_url . '/index.html', 'w') or die('Can\'t open file');
fwrite($handle, $html);
fclose($handle);
But since I'm running MAMP, $_SERVER['DOCUMENT_ROOT'] equals Applications/MAMP/htdocs, so that won't work. So what can I do to write a file in PHP that would work in both a production environment and a local environment?

Use the FCPATH or APPPATH constants.
FCPATH will give you the path to the directory where the front controller is located (index.php).
APPPATH will give you the path to the application directory.
Usage:
fopen(FCPATH . 'path/relative/to/frontcontroller/etc/' . $name_url . '/index.html', 'w') ...

Related

Storage/File delete methods don't delete the file from storage

I am uploading images to storage/uploads and have a queue job that uploads those images to AWS. Here is the code of the job:
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
if (Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'))) {
File::delete($path);
}
Once the image has been uploaded to AWS the job is supposed to delete the image from storage/uploads but it's not doing that. The images are successfully uploaded to AWS. I tried to delete specific files directly without the if-statement but nothing seems to work. I even tried with Storage::delete but that didn't work either. Could someone point me in the right direction please?
You may use
Storage::delete('upload/profile/' . $fileName);
See Documentation
If your specifying the full directory path of the file you can use unlink a native way to delete files from the server.
$path = storage_path() . '/uploads/' . $this->fileId;
$fileName = $this->fileId . '.png';
$isUploaded = Storage::disk('s3images')->put('profile/' . $fileName, fopen($path, 'r+'));
if ($isUploaded) {
unlink($path);
}
Documentation: unlink php

File storage not working. Laravel 5

I'm trying to save a .txt file in a folder named txts inside my public folder but the method i'm using is not working:
$txt_nombre = 'nomina_'. $extras['desde'] .'_'. $extras['hasta'] .'.txt';
$txt_contenido = '';
foreach ($personal as $per) {
$txt_cadena = 'BNC ' . $per->cuenta_nomina . ' ' . $per->netos . ' ' . $per->cedula_identidad;
$txt_contenido .= $txt_cadena . "\n";
}
Storage::put('public/txts/'. $txt_nombre, $txt_contenido);
It doesn't even throw an error, my function keeps running but the file is not being saved. Any idea why?
According to the Documentation
By default, the public disk uses the local driver and stores these files in storage/app/public.
Looking at your code, assuming that you haven't changed default configuration, your file should be created in storage/app/public/public/txts/ folder.
I am not sure how Laravel's Storage abstraction handles non-existing folders so you might want, for test, try using just Storage::put('file.txt', "content") and see if it's being created in storage/app/public folder

Magento: include and app/code/core from another location(above webroot)

I am looking for a way to include magentos app/code/core from above webroot.
It is no problem to do this with /lib, just add the path at the end of the include path in your php.ini
Something like
.:/existing/path:/home/yourdomain/magento/lib
For demo-stores or development servers this means saving 30mb or more than 3000 files per installation.
Therefore I would love to do the same with app/code/core as it is another 26mb and more than 5000 files per installation.
Did open a thread here
http://www.magentocommerce.com/boards/viewthread/457181/
Any help would be appreciated.
One way to do this...
Modify app/Mage.php where you will find the following lines that add paths to check when you include files...
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
$paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
$paths[] = BP . DS . 'lib';
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Mage::registry('original_include_path'));
You could change BP to wherever you move the code, or change these entirely.
If you move the entire app directory, then change your index.php file in your web base directory. You'll see this line
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
You'll want to change this to wherever you moved it. Mage.php is what bootstraps the whole operation.

Magento trying to create CSV inside a directory throwing FATAL error Fatal error: Class 'Mage_Helper_Data' not found

I have a script that creates a file and currently puts it inside media folder, but I'd like to have it inside media/files so here is my code
$filePath = Mage::getBaseDir('media') . DS .'files'. 'mycsvfile.csv';
$fileName = 'mycsvfile.csv';
$mageCsv = new Varien_File_Csv();
$mageCsv->saveData($filePath, $ordersRow);
Mage::app()->getFrontController()
->getResponse()
->setRedirect(Mage::helper('adminhtml')->getUrl('mycsvfiles/adminhtml_download/index/', array('file' => $fileName )));
I don't see any errors in your code - aside from
$filePath = Mage::getBaseDir('media') . DS .'files'. 'mycsvfile.csv';
should probably be
$filePath = Mage::getBaseDir('media') . DS .'files'. DS . 'mycsvfile.csv';
since files is a directory. The error you see is probably thrown somewhere else. Try to check your etc section .xml files - the directives translate="label" module="modulename" where modulename is a helper alias. I've had a couple of such errors the other day.

Ajax Post Security Question

so i have a problem, i have this code:
$params = "'plname=" . $player->username . "&plmiss=" . $player->miss . "&plmaxdmg=" . $player->maxdmg . "&plmindmg=" . $player->mindmg . "&plhp=" . $player->hp . "&plmhp=" . $player->maxhp;
$params .= "&enname=" . $enemy->username . "&enmiss=" . $enemy->miss . "&enmaxdmg=" . $enemy->maxdmg . "&enmindmg=" . $enemy->mindmg . "&enhp=" . $enemy->hp . "&enmhp=" . $enemy->hp . "'";
buttonform("pvm.php","Attack",$params);
buttonform function:
function buttonform($page,$texto,$params)
{
?><input type="button" onclick="ajaxpost('menu','<?php echo $page;?>',<?php echo $params;?>);" class="button" value="<?php echo $texto;?>"><?
}
so you guessed it the function will create a button that when be clicked will send an ajax request for the pvm.php + $params.
but the problem is that $params is confidential and should not be avaiable to change. but if we enter in the page code (ive done this with google chrome developer tools) we can change those variables to what we want, and that is what i dont want. if anyone can help me to make those variables not avaiable for change, THANKYOU!
Anything loaded into the user's browser is available for change. You'll have to store that information server-side.
To that end, take a look at PHP sessions:
http://www.w3schools.com/PHP/php_sessions.asp
http://www.php.net/manual/en/book.session.php

Resources