unclear regarding uploading of csv file in php - php

So I want to:
upload a csv file which will contain a list of student numbers, one to each line (392232, per line).
populate an array with the student numbers (as i already have a process in place of looking up ids from an array of student numbers and storing etc if they were to add students manually)
I have been lookin at a tutorial found here.
however I am slightly confused with this:
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){...
where does he establish 'tmp_name' from?
anyway, if somebody could explain how I should be going about this I would appreciate the help.
many thanks,
EDIT: added progress of where it is not working.
if(isset($_POST['csv_submit'])){
if(isset($_FILES['csv_file']) && is_uploaded_file($_FILES['csv_file']['tmp_name'])){
//upload directory
$upload_dir = "/ece70141/csv_files/";
//create file name
$file_path = $upload_dir . $_FILES['csv_file']['name'];
//move uploaded file to upload dir
// GETTING THE ERROR BELOW.
if (!move_uploaded_file($_FILES['csv_file']['tmp_name'], $file_path)) {
//error moving upload file
echo "Error moving file upload";
}
print_r($_FILES['csv_file']);
//delete csv file
unlink($file_path);
}
}

$_FILES is a magic superglobal similar to $_POST. It's an array of every file that's been uploaded in the last request, and where that file is stored (tmp_name).
tmp_name is basically generated by the web server to let PHP know where they've stored the file.
You have the following items available to you in each element of the $_FILES array:
name (Original Name of the file)
type (MIME Type of the file, ie. text/plain)
tmp_name (Path to the uploaded files' temporary location)
error (Error occurred when uploading, 0 when no error)
size (Size of the uploaded file, in bytes)
From what I can see in your code, this will work perfectly fine and as discussed in comments, I think the issue lies in your HTML.
The tutorial that you linked to has an incorrect <form ..> tag definition. For file uploads, you're required to set the enctype attribute, below is what it should look like:
<form action="" method="post" enctype="multipart/form-data">

Related

PHP letting the code add the uploader's last name to the image file he is uploading from a form

Got the server, got the domain, got the code, getting the images successfully, making the products for the customers from the image files they upload. Yay!
Problem: all my image names are image_0001 etc.
Customers can't rename image files from iPhones and do not care to from PCs.
So I was thinking about putting a short form on the upload page asking for customer's last name and having the PHP code attach that name to the image file(s) being uploaded.
If it's not possible, I'm sorry for the inconvenience.
You can rename files after they have been saved to your server, check out the PHP manual for the rename function - http://www.php.net/manual/en/function.rename.php, or just while you are moving them from the tmp directory, you can specify a different name for the uploaded file. See http://www.php.net/manual/en/function.move-uploaded-file.php
Be careful to include something in your code for dealing with naming conflicts.
This one might help :
$imagename = basename($_FILES['file']['name']);
$ext = pathinfo($imagename , PATHINFO_EXTENSION); //we want to change the file name but not the extension
$newImagename= $imageName.$username.'.'.$ext; //assuming you hold the username in $username
if (move_uploaded_file($_FILES['file']['tmp_name'], "/path/{$newImagename}"))
{
....
}

How to upload file to amazon s3 using MAMP?

I'm trying to push a file to a Amazon s3 filebucket.
I'm posting the file through an html form.
I try to generate a path to the file like this($file is a part of a foreach, because i need to support multiple files in a form-submit.)
$file['tmp_name'].'/'.$file['name'];
this outputs a filepath like this
/Applications/MAMP/tmp/php/phpZDcVQv/pdf.pdf
/Applications/MAMP/tmp/php/ exists, but nothing is inside it. I have set access read and write for everyone to that folder.
I use a library to post the images to Amazon: https://github.com/tpyo/amazon-s3-php-class It also complains that the filepath i have provided doesn't exist. It's running a check like:
if (!file_exists($file) || !is_file($file) || !is_readable($file))
How come the files aren't added?
Am I referencing the wrong folder? The file with the code is in /web/projectname/
Someone on the internet said something unclear about php removing the temp-file directly. Is this after the response has been run? Do I need to address this in some way?
The most simple code that generates the problem:
foreach ($_FILES as $file) {
$filepath = $file['tmp_name'].'/'.$file['name'];
if(file_exists($filepath)){
echo 'true <br />';
}else{
echo 'false <br />';
}
}
This echo:es false even if files have been uploaded.
$filepath contains the path i described above.
as the manual states:
$_FILES['userfile']['tmp_name']
The temporary filename of the file in which the uploaded file was stored on the server.
and
$_FILES['userfile']['name']
The original name of the file on the client machine.
this means that file_exists($file['tmp_name']) should be true.
The path $file['tmp_name'].'/'.$file['name'] is bogus, since $file['name'] is there only for informing you of the original name, but this name is not used while saving the uploaded file on the server.
So in your example /Applications/MAMP/tmp/php/phpZDcVQv is actually the uploaded file.

verify file name at upload with only a given file name

How can i verify a file name when uploading e.g. xyz.xlsx for this specific file and no other? Thus the file is uploaded if and only if its name is xyz with extension xlsx "xyz.xlsx".
If I understand what your looking for, just grab the filename and test it.
Example:
if($_FILES['uploadedfile']['name'] == "xyz.xlsx")
http://us2.php.net/manual/en/features.file-upload.post-method.php
When you do an upload with PHP, you get a $_FILES array (kind of like $_POST). You'll find the original filename in $_FILES['userfile']['name']. You can verify that as MyGlass suggests in his answer.
if($_FILES['uploadedfile']['name'] == "xyz.xlsx")
Additionally, codeignitor provides a file upload class that can be used to eliminate some of the boiler plate of file uploads here: http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
Use $this->upload->data() to get the array with the filename info (the key is 'file_name').
$data = $this->upload->data();
if($data['file_name'] == "xyz.xlsx")

Saving a PDF file using PHP

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.

PHP read from uploaded text file?

If I upload a text file via a form, is it possible to output its contents directly from the $_FILES variable rather than saving it onto the server first? I know this is a security risk, but it will only be run on a local machine.
Doing
file_get_contents($_FILES['uploadedfile']['tmp_name']);
is valid however you should also check to make sure that the file was uploaded through a form and that no errors occurred during upload:
if ($_FILES['uploadedfile']['error'] == UPLOAD_ERR_OK //checks for errors
&& is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { //checks that file is uploaded
echo file_get_contents($_FILES['uploadedfile']['tmp_name']);
}
A helpful link is http://us2.php.net/manual/en/features.file-upload.php
The file is saved to temp directory the moment it's uploaded, but you can use $_FILES['uploadedfile']['tmp_name'] to read it without having to save in a permanent place.
Unfortunately, no. At least not through the $_FILES variable. Sorry.
EDIT: It is always saved as the temp file in $_FILES and you'll always have to use that one for content.

Categories