Iam working on an inhouse project where there is a multi part file uploading function is built. Iam running a windows 7 with wamp 2.5
The multiple file upload works perfect and the files are being uploaded into the specified directories. The upload directory has been shared on a Lan network so that the other users can also access the uploaded file. But the uploaded files cannot be viewed from another computer connected in the Lan. There are lot of folder creations and the Lan systems are able to browse through the folders. But the files are not able to be viewed. Iam pasting the code below
//File upload section
if(count($_FILES['upload']['name']) > 0){
if (!file_exists('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername))
{
mkdir('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername, 0777);
}
if (!file_exists('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername.'/01_'.$product))
{
mkdir('Other_Orders/'.$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername.'/01_'.$product, 0777);
}
$pathtoupload= "Other_Orders/".$currentyear.'/'.$currentmonth.'/'.$currenttime.'/'.$ordernumber.'_'.$customername."/01_".$product;
//Loop through each file
for($i=0; $i<count($_FILES['upload']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
//save the url and the file
$filePath = $pathtoupload."/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
//insert into db
//use $shortname for the filename
//use $filePath for the relative url to the file
}
}
}
}
I have found the answer myself
The files uploaded through the wamp will never get the full permission from the windows. The only way out is to add a code to copy the uploaded file by renaming it with an underscore prefix or something like that so that the new file will get the full permission and will be readable and editable by the other windows users connected to the Lan. The code will be somewhat like below (in connection with the code mentioned in the question)
//Loop through each file
for($i=0; $i
//Make sure we have a filepath
if($tmpFilePath != ""){
//save the filename
$shortname = $_FILES['upload']['name'][$i];
$share_name = "_".$shortname;
//save the url and the file
$filePath = $pathtoupload."/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $filePath)) {
$files[] = $shortname;
$source_dir = $pathtoupload."/".$shortname;
$target_dir = $pathtoupload."/".$share_name;
copy($source_dir,$target_dir);
unlink($source_dir);
Related
So, I want to create a system where user uploads in a zip file the files of a 3d model and the model can be shown, stored, etc
So, I got the file, I place it into a folder, permanently, And unzip it into another temp folder, just to see if it is a 3d model.
I tried like this:
$target_dir = "upload/";
$targetfilename = rand().$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $target_dir.$targetfilename);
//unzip the file into temp folder
$tmp_dir = $target_dir.rand();
mkdir($tmp_dir);
chmod($tmp_dir, 0777);
//chmod($targetfilename, 0777); //this not working, maybe isn't the right way
$zip = new ZipArchive;
$res = $zip->open($targetfilename);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($tmp_dir);
$zip->close();
echo 'SUCCESS';
} else {
echo 'ERROR';
}
I do not get any errors, but the zip can't be unzipped. Any idea? How can I resolve this?
Isn't $targetfilename is in $target_dir folder?
If so, changing
$res = $zip->open($targetfilename); to
$res = $zip->open($target_dir.$targetfilename); might solve your problem.
So I have created a small script that allows a user to upload multiple images of their car. I then process these images and store the file path in a database, and create a folder to store the images in, inside my 'vehicleImages' folder. I keep running into a problem though, so as you can see I check to see if the folder exists and if it doesn't I create one. Then the images get processed and in theory should be stored inside that folder.
The problem i'm running into is, it creates the folder like it should e.g. XX00VVV. But instead of storing the images inside 'vehcileImages/XX00VVV' it stores the images inside 'vehicleImages' not inside the correct folder. I've checked the server and the folder 'XX00VVV' is definetly being created but for some reason the images aren't being stored inside. I think it's where I change the target path but I can't figure out why. Can someone give me a few pointers/hints as to where I've gone wrong please?
$imagePath = "../../images/vehicleImages/".$vehicleReg;
$fileName = $vehicleReg;
if (!file_exists($imagePath)) {
mkdir($imagePath, 0777, true);
}
//Image Upload Section
$j = 0; //Variable for indexing uploaded image
$target_path = $imagePath; //Declaring Path for uploaded images
for ($i = 0; $i < count($_FILES['file']['name']); $i++) { //loop to get individual element from the array
$validextensions = array("jpeg", "jpg", "png"); //Extensions which are allowed
$ext = explode('.', basename($_FILES['file']['name'][$i])); //explode file name from dot(.)
$file_extension = end($ext); //store extensions in the variable
$target_path = $target_path.md5(uniqid()).
".".$ext[count($ext) - 1]; //set the target path with a new name of image
$j = $j + 1; //increment the number of uploaded images according to the files in array
if (($_FILES["file"]["size"][$i] < 100000000) //Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) { //if file moved to uploads folder
echo $j.
').<span id="noerror">Image Uploaded Successfully!.</span><br/><br/>';
} else { //if file was not moved.
echo $j.
').<span id="error">Please Try Again!.</span><br/><br/>';
}
} else { //if file size and file type was incorrect.
echo $j.
').<span id="error">***Invalid File Size or Type***</span><br/><br/>';
}
}
//End Image Upload Section
Image of Website Directory
so the problem is in first line in your code $imagePath
$imagePath = "../../images/vehicleImages/".$vehicleReg;
try to add ."/" to the end of line
like this:
$imagePath = "../../images/vehicleImages/".$vehicleReg."/";
the explain of the solution is:
when you didn't add / in the end of path, you make it merged with filename , eg XX00VVVfilenam.png
I have a FPDF script that submits a PDF and moves it to a folder on the server.
I have a upload field in the form before the FPDF script is run named "file"
i am trying to move it to the same folder as the generated PDF.
below is my code: (the PDF is generated and moved to the folder but nothing happens with the uploaded file)
mkdir("claims/$name", 0777);
$move = "claims/$name";
foreach ($_FILES["files"]["tmp_name"] as $key => $value)
{
$tmp_name = $_FILES["files"]["tmp_name"][$key];
$name2 = $move ."\\".basename($_FILES["files"]["name"][$key]);
move_uploaded_file($tmp_name, $name2);
}
$filename = "claims/$name/$name.pdf";
$pdf->Output($filename, 'F');
header('Location: home.php');
I was able to get this working with a few changes to my code. I also got it to work with multiple uploads.
// make the directory
mkdir("claims/$name", 0777);
$total = count($_FILES['files']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['files']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "claims/$name/" . $_FILES['files']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
}
}
}
I have the following code, which as you can see i use to create a new directory then unzip a file.
<?php
function unzip_to_s3() {
// Set temp path
$temp_path = 'wp-content/uploads/gravity_forms/1-9e5dc27086c8b2fd2e48678e1f54f98c/2013/02/tmp/';
// Get filename from Zip file
$zip_file = 'archive.zip';
// Create full Zip file path
$zip_file_path = $temp_path.$zip_file;
// Generate unique name for temp sub_folder for unzipped files
$temp_unzip_folder = uniqid('temp_TMS_', true);
// Create full temp sub_folder path
$temp_unzip_path = $temp_path.$temp_unzip_folder;
// Make the new temp sub_folder for unzipped files
if (!mkdir($temp_unzip_path, '0755', true)) {
die('Error: Could not create path: '.$temp_unzip_path);
}
// Unzip files to temp unzip folder, ignoring anything that is not a .mp3 extension
$zip = new ZipArchive();
$filename = $zip_file_path;
if ($zip->open($filename)!==TRUE) {
exit("cannot open <$filename>\n");
}
for ($i=0; $i<$zip->numFiles;$i++) {
$info = $zip->statIndex($i);
$file = pathinfo($info['name']);
if(strtolower($file['extension']) == "mp3") {
file_put_contents(basename($info['name']), $zip->getFromIndex($i));
} else {
$zip->deleteIndex($i);
}
}
$zip->close();
}
unzip_to_s3();
?>
The unzip code was courtesy of #TotalWipeOut from one of my other posts. It currently unzips just mp3 files to my base directory, but i want to put them in my newly created folder.
I'm very new to PHP so have been trying my best with this, but i can't figure out how to change the file_put_contents(basename($info['name']), $zip->getFromIndex($i)); line to get it to put the files in my new folder?
As Marc B. mentioned you need to include the path to the directory you are putting the file in.
using your code:
file_put_contents($temp_unzip_path."/".basename($info['name']), $zip->getFromIndex($i));
I would also suggest reading a little more about the basics of PHP.
Hey i have a system were in uploading a file. I have a script I've found online and it seems to work well.
Here is the PHP code:
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && ($_FILES["uploaded_file"]["size"] < 350000))
{
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname))
{
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname)))
{
echo "It's done! The file has been saved as: ".$newname;
}
else
{
echo "Error: A problem occurred during file upload!";
}
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: Only .jpg images under 350Kb are accepted for upload";
}
}
else
{
echo "Error: No file uploaded";
}
No this works fine if i want to upload a jpg file. But i want to be able to put the file into another directory. because at the moment the upload page is for admin users, they are on a subdomain called admin.mysite.com but the location i want the file to go to is in the members section which is mysite.com/members/video/
Now there are a few bits of code that im not 100% with like "dirname(FILE)" what does this do? I guessed it would get the current locations, but i've changed the whole line where so it looks like this:
$newname = '../mysite.com/members/video/'.$filename;
and
$newname = 'http://www.mysite.com/members/video/'.$filename;
But nothing. Anyone know how i can change this code so i can copy the file to a new directory?
Thanks for the help.
Change $newname to whatever location you want!
//Determine the path to which we want to save this file
$newname = dirname(__FILE__).'upload/'.$filename;
dirname(_ FILE _) returns the current directory of the file, in this case, file_upload.php
So, in this script, the $newname will save the uploaded file to /upload/name_of_new_file_uploaded.ext.
You should try to use realpath() instead dirname. Like this:
$newname = realpath("../../members/video/") . $filename;
depending on you file structure, add/remove the dots.
PS: Remember to change folder permissions, so the php can write on a folder.
For security, you should
Put the file in a location that is not accessible by the general web. /home/uploadedfiles/
Change the name of the file. Store the name of that file in a database and don't let the end users see that actual name.