Switch/select default directory as store folder - php

Since I'm using dropzone.js to store images to a directory I would like to know if there is a possibilty within PHP or other to first SELECT the destination or store folder before the file is dropped. Normally, there is a default path to the folder defined in PHP such as:
// upload.inc.php
<?php
include("../inc/config.inc.php");
$ds = DIRECTORY_SEPARATOR;
$storeFolder = '../../gallery/samples';
if (!empty($_FILES)) {
$tempFile = $_FILES['file']['tmp_name'];
$targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;
$targetFile = $targetPath. $_FILES['file']['name'];
move_uploaded_file($tempFile,$targetFile);
}
?>
Now, the store folder is predefined. What I have in mind is to first move the file into the dropzone field and then secondly select, either by Jquery, html or somehow, the folder to which the file is dropped off to. I don't want to change the path manually constantly on the upload script. How can I establish this? JStree for instance looks pretty well to have it done, idk.

Assuming you have a name stored in JavaScript that you want to use as the folder to store the uploads, you'll need to create a hidden HTML form element that gets populated with JavaScript:
HTML:
<input type="hidden" id="storefolder" name="storefolder">
JS:
var storefolder = document.getElementById["storefolder"];
storefolder.value = [VARIABLE]; // Whatever changes the folder needs to pass through here
PHP:
$storeFolder = $_POST["storefolder"];
Now your JavaScript variable will become your PHP variable, without the user ever seeing it outputted in the form. Note that you could also make the input field visible (with type=text), and simply use its value to pass directly through to the PHP if need be, bypassing the need for any JavaScript.
Hope this helps!

Related

Image uploader working perfectly for an insert form but not for an update form

I know there are already many similar questions like this and I apologize in advance for adding to the file, but I am a little short on time to do research and I need quick help. I am trying to finish an overdue assignment and my image upload function is working perfectly when I add a product, but not when I update it. I have no idea why. My code to update the image is here:
require_once 'file-util.php'
// Check if the file exists before setting it
if (isset($_FILES['imageFile1'])) {
// Retrieve the name of the file based on what it was called on the client computer
$filename = $codeInput . '.png';
// Make sure the filename exists
if (!empty($filename)) {
// Store the temporary location of where the file was stored on the server
$sourceLocation = $_FILES['imageFile1']['tmp_name'];
// Build the path to the images folder and use the same filename as before
$targetPath = $image_dir_path . DIRECTORY_SEPARATOR . $filename;
// Move file from temp directory to images folder
move_uploaded_file($sourceLocation, $targetPath);
}
}
This is the exact same code that I have in my insert_product file.
And my file_util is here:
$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;
Everything else works perfectly, but it is just this little thing that isn't seeming to do anything, so it seems to me like there's a little detail I'm missing for this to work in update_product. Is there something else I need to do to get this to work, or is it something else I'm unaware of?
Edit: Turns out that I just forgot to set the encryption type in my add_product_form. If anyone else has this silly issue, double check your forms for this near the top of the body:
<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">
You need to check if your updating form tag has the proper enctype attribute value...
and please be aware to use more validation on the uploaded file, your checking for file name exists or not will always be true as you are setting a value for it in the previous line.
Apparently, my code was right but I just forgot to go "enctype="multipart/form-data" in update_product_form.php.

how to get the full path and store it in a variable

I would to create a program that upload a file and whatever it is inside the file i would output it through the use of the textarea. But my problem is I can't get the full path of the uploaded file due to security of the browser. I tried using this How to get the path of a file before its uploaded? as a reference but it only show the fakepath. Main problem is I can't get the full path to store it in a variable. Any idea how to do this?
Im using this code to get the file name
<?php
if(isset($_POST['btnSubmit']))
{
$fileName = $_POST['file'];
$fullPath = (realpath).$fileName;
--here there must be a variable that will hold the fullpath but the $fullPath is wrong way to declare.
echo $fullPath."\n\n";
}
?>

Moving an image file in php from a tmp directory to a permanent one (using Laravel)

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']

Interpret Code - Non PHP Programmer

What is this code doing?
<?php
if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
move_uploaded_file($tempFile,$targetFile);
echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
}
?>
It is basically uploading a file and echoing the target file's name.
There should also be something like an HTML form to send the file to this script.
It accepts an user uploaded file, and puts it in your webroot in a folder, specified by the user. Then it outputs the path of the uploaded file.
This code is to upload a file in the destination directory. Is there anything else do you want to understand.
This will upload the file in the path you have in variable $_REQUEST['folder']
Storing files uploaded (possibly via HTML form) onto server.
It's accepting a file posted by a HTML form and uploading it to a certain directory within a server. After that it's displaying the location of the file on screen (in the browser).

Uploadify JQuery & PHP/MySQL - Change file name on upload

I'm using Uploadify as part of a form. Let me give you a bit of background, it may help. I have a form where a user can add "projects" to a website. First they type in the name of the project and a description. On submit, this updates a PHP/MySQL database table named "project" and is given an ID.
The user can then upload files to a location on the server. I wish to add the project name onto the start of the file name for upload AND the project ID (which I need to add to the database) before upload begins, then when upload completes add the file details to a database table "image" - linked to "project" via the project ID.
I know I'm kinda bouncing back and forth a lot, I need to know how to do this. Two database tables to update, one on form submit and one on file-upload. I need to pass the project name and ID to the uploadify upload script.
SOLUTION:
I had to use the below uploadify method to send the Project ID to the uploadify script, having previously filled variable pid with the mysql_insert_id result:
'onSelectOnce': function(event,data) {
$('#file_upload').uploadifySettings('scriptData', {'pid': pid});
}
I could then receive the pid variable in the PHP uploadify script using a simple post:
$pid = $_POST['pid'];
It was then a matter of running a select within this script to get the data I needed for the database (the project alias) and adding it to the filename before upload:
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
Hopefully this will help people in the future.
I had to use the below uploadify method to send the Project ID to the uploadify script, having previously filled variable pid with the mysql_insert_id result:
'onSelectOnce': function(event,data) {
$('#file_upload').uploadifySettings('scriptData', {'pid': pid});
}
I could then receive the pid variable in the PHP uploadify script using a simple post:
$pid = $_POST['pid'];
It was then a matter of running a select within this script to get the data I needed for the database (the project alias) and adding it to the filename before upload:
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/' . $alias . '-';
$targetFile = str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
Hopefully this will help people in the future.
In the uploadify script there is part that gives the syntax for the file being handled by the upload form. I don't have the script on hand, but uplodify hs a onbefore complete callback and an on complete call back features.
use the before complete and append the name to an ajax request that will save it to your database, from there just perform 2 queries, upload the name of the image and set user_id to the ID of the user thats probably from ur session.
var = file_before_upload_name: filename // here use the sytax that Uploadify uses to capture the name of the file
var = file_after_upload_name: filename // here use the sytax that Uploadify uses to capture the name of the file
then on the aftercomplete callback use an ajax request and set
uid : uid //from a session
before: file_before_upload_name,
after : file_after_upload_name
in the ajax your queries would look like
mysql_queries("INSERT INTO `tbl-projects` SET `user_id` = {$_POST['uid']}, `file` = {$_POST['after']}");
//another query here to set the data to your other table that relates to tbl-projects
Try this
http://programmintalk.blogspot.com/2011/02/jquery-uploadify-rename-uploaded-file.html

Categories