I want to create a new file along with the the creation of a record in my db. The db part works fine, but the file part isn't working. I want to create a file in another folder in the root directory and I used the same code with changes in the file name. Despite setting ini_set('display_errors',3) the code doesn't return any errors, I've also tried fopen($file_name,"w") or die("unable to open file"), still the code ran. I've given IIS_IUSRS full control over the directory. Here is my code
$data = mysqli_fetch_array(mysqli_query($conn,"Select * from content where link='$url'")); //fetch data from db
$id = $data['id'];
$file_name="/files/$id.html"; //sets file name
$file = fopen($file_name,"w"); //creates the file
fwrite($file,$_POST['desc']); //writes to the file
fclose($file); //closes the file
The problem was PHP wasn't going to the root directory. I added $root = $_SERVER['DOCUMENT_ROOT'] and replaced $file_name="/files/$id.html" with $file_name="$root/files/$id.html"; and it worked like a charm.
Related
I have some encrypted responses that I convert to a Zip file in my Laravel application. The function below downloads the API response, saves it as a Zip file, and then extracts it while I read the folder's contents. In my local environment, it works well. However, the Zip file is not getting saved to the storage folder on the live server. No error is being shown, only an empty JSON response. Please, what could be the cause?
public function downloadZipAndExtract($publication_id, $client_id)
{
/* We need to make the API call first */
$url = $this->lp_store."clients/$client_id/publications/$publication_id/file";
$file = makeSecureAPICall($url, 'raw');
// Get file path. If file already exist, just return
$path = public_path('storage/'.$publication_id);
if (!File::isDirectory($path)) {
Storage::put($publication_id.'.zip', $file);
// Zip the content
$localArchivePath = storage_path('app/'.$publication_id.'.zip');
$zip = new ZipArchive();
if (!$zip->open($localArchivePath)) {
abort(500, 'Problems experienced while reading file.');
}
// make directory with the publication_id
// then extract everything to the directory
Storage::makeDirectory($publication_id);
$zip->extractTo(storage_path('app/public/'.$publication_id));
// Delete the zip file after extracting
Storage::delete($publication_id.'.zip');
}
return;
}
First thing I'd check is if the storage file is created and if it isn't created, create it. Then I'd look at your file permissions and make sure that the the groups and users permissions are correct and that you aren't persisting file permissions on creation. I've had many instances where the process that's creating files(or trying) is not in the proper group and there is a sticky permission on the file structure.
So i'm currently trying to create a code which simply creates and publishes a file to my webroot, modifies and writes to that file, and then finally change the location of the file to another directory/folder using move_uploaded_file()
This is my code so far
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,'$dir/$title.txt');
fwrite($myfile, $_POST['textarea11']);
fclose($myfile);
The code doesn't work, i've tried echoing move_uploaded_file() and it returned nothing, however the file was uploaded but it's location just wasn't changed.
$dir is defined as $dir = __DIR__.'/../uploads/'; and $title is define as $title = $_POST['title'];
move_uploaded_file() can only be used if you are submitting a multipart form and you want to save the uploaded file.
What you probably need is this:
http://php.net/manual/en/function.rename.php
Change your given code as
$dir = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'uploads';
$myfile = fopen($_POST['title'].".txt", "w");
move_uploaded_file($myfile,"$dir".DIRECTORY_SEPARATOR."$title.txt");
fwrite($myfile, $_POST['textarea11']);
In your code
move_uploaded_file($myfile,'$dir/$title.txt');
php variable $dir and $title value is not coming. and value of $dir is consisting '/' and you are adding one more to make full file path too.
Always use directory separator to run in all Operating System. some OS use '/' and some OS use '\'.
I am trying to move a file from the command directory it currently sits in to a new directory called product_upload however the code below does not work properly
//The following creates and saves the file in the directory - this bit works fine. It saves the file in the command directory
$file = fopen($filenamecsv, "w+");
fwrite($file,$contents);
fclose($file);
////The following does not work
$filenamecsv_move = "command/Product_category_list".date("j-m-Y_H.i"). ".csv";
$filenamecsv2_move = "command/product_upload/Product_category_list".date("j-m-Y_H.i"). ".csv";
rename($filenamecsv_move ,$filenamecsv2_move );
Probably directory command/product_upload doesn't exists and rename function won't create it. In this case you have to manually create this directory or add mkdir in your script.
Look at below script. If you have only command directory and commend mkdir line rename function won't rename file and return false (var_dump added at the end).
<?php
$filenamecsv_move = "command/Product_category_list".date("j-m-Y_H.i"). ".csv";
file_put_contents($filenamecsv_move,'testdata');
$filenamecsv2_move = "command/product_upload/Product_category_list".date("j-m-Y_H.i"). ".csv";
if (!file_exists('command/product_upload')) {
mkdir('command/product_upload', true);
}
$result = rename($filenamecsv_move ,$filenamecsv2_move );
var_dump($result);
You should also turn on error_reporting to display errors when you are testing your script. I had the following warning:
Warning:
rename(command/Product_category_list29-05-2014_13.40.csv,command/product_upload/Product_category_list29-05-2014_13.40.csv):
in D:\DaneAplikacji\WampServer\www\rename\index.php on line 12
I am trying to move an image file previously uploaded into a tmp directory to a permament location. I have used this tutorial as a starting point:
maxoffsky.com/code-blog/uploading-files-in-laravel-4/
I have amended the code as I am uploading the files earlier and accessing them later through a url provided by the form. The code below shows what I am doing:
$form_element = "image_1"; // hidden form element containing the tmp location
$tmp_path = $form_data[$form_element]; // gets the tmp url from hidden element
$file = fopen($tmp_path, 'r'); // opens the file
$destinationPath = 'images/adverts/'.$advert->id; // specifies a new folder
$filename = $file->getClientOriginalName(); // retrieves the name of the file
$upload_success = $file->move($destinationPath, $filename); // moves the file to its new location
The problem I am having is that in the tutorial, they use the code:
$file = Input::file('file'); // Laravel syntax for $_POST['file']
This retrieves the file from the html form itself. From there the functions $file->getClientOriginalName() and $file->move() work correctly.
However, in mine, as my form doesnt provide an actual file, just a link to one, I am trying to access the file and perform the same operations, however I get this error:
Call to a member function getClientOriginalName() on a non-object
I dont think that fopen() is returning the same type as $_POST['file'] hence it isnt working.
How can I make my code work?
Many thanks
I had the same issue, just add in the Form::open in array the enctype:
enctype='multipart/form-data'
and your problems are solved! I have forgotten it and banging my head to the wall after that I started harder to bang it when I saw it.
PS. You have a little misconception. Input::file('file') it's not syntax for $_POST['file'], but for $_FILES['file']
I have a problem with saving PDF files to folders on my server. The code worked at one time and now it doesn't. What I want it to do is to check if someone is trying to upload a PDF when a form is submitted, and if there is a PDF in the file field it uploads it and then saves the path to the mysql database. Code is below:
if (!empty($_FILES['pdf'])){
$idir = "../files/PDF/"; //my directory file is supposed to be saved in
$randomd=rand(0000000,9999999); //creates a random number as filename
$domain = "http://".$_SERVER['HTTP_HOST'];
$file_ext = strrchr($_FILES['pdf']['name'], '.'); grabs file extension. my code checked if the file was a pdf a different way and neither seems to work.
$destination=$randomd.$file_ext; //new filename
if ($file_ext=='pdf') {
move_uploaded_file($_FILES['pdf']['tmp_name'], "$idir" . $destination);
$pdf= $domain."/files/PDF/".$destination; } else { echo("File type not supported.");
mysql_query("UPDATE tbl_listings SET pdf='$pdf' WHERE listing_id='$lid'");
}
The if not empty does not work and it always tries to upload a file, but when I check the folder nothing is in there and it doesnt update the mysql.
$_FILES['pdf'] will never be empty(when the form has been submitted), no matter if a file has been selected or not, it will always return an array.
Check $_FILES['pdf']['error'] , it will be 4 when no file has been uploaded.