I used FileEntity httppost to post a file to a php apache server,but I do not want to change the name of the file.
How can I get the name of the file from the server using php? I have already get the file:
$filename='test.zip';//自定义的名字
file_get_contents('php://input');
file_put_contents($filename,$data);
HTML (test.html):
<!-- Make sure you have enctype and a name on your file input -->
<form method="post" action="test.php" enctype="multipart/form-data">
<input type="file" name="test" />
<button>Submit</button>
</form>
PHP (test.php):
// Since we've set our file input to name 'test' and
// we've posted form-data, we can use $_FILES
print_r( $_FILES['test'] );
Just use $_FILES.
Array
(
[name] => test.jpg
[type] => image/jpeg
[tmp_name] => /tmp/pasd41l3FmFr
[error] => 0
[size] => 15476
)
glob — Find pathnames matching a pattern
foreach (glob("*.*") as $filename) {
print $filename. "\n";
}
Related
we can set PHP variable as the value of input in HTML form like this
<input type="number" name="price" value="<?php echo $editprice;?>" >
but this doesn't work for input with type file.
I try it this way
<?php
$sqlch15="SELECT image1 FROM pc where id=$idtoe";
$resultch15= mysqli_query($db, $sqlch15);
while ($row = mysqli_fetch_array($resultch15))
{
$editimg1 = "<img src='images/" .$row['image1']."'>";
}
?>
<input type="file" name="image1" value = "<?php echo $editimg1;?>">
but it doesn't work what is my mistake help me.
The value property contains the complete path of the file.
The value property of the input:file element is read-only, because of security reasons.
if you press that button you upload a picture into the /tmp folder of the server (or another folder designed for that)
You will create a Array variable called $_FILES['image']
[_FILES] => Array
(
[image1] => Array
(
[name] => example.png
[type] => image/png
[tmp_name] => /tmp/phpq0JHjV // that is the upload folder
[error] => 0
[size] => 10847
)
)
So the only thing that would make a little sense is to fill in the name of the file
<input type="file" name="image1" value="<?=$_FILES['image']['name'];?>">
But as I said it makes no much sense because the info about the path to this file on the client is missing.
I have a form.When a user clicks on add a file. Two input fields are appended. One with the input name of the file to be uploaded and another with input field for the file itself
<form>
<label>File 1</label>
<input type="text" name="fileName[]"/>
<input type="file" name="file[]"/>
<label>File 2</label>
<input type="text" name="fileName[]"/>
<input type="file" name="file[]"/>
.
.
.
.
<input type="submit" value="submit"/>
How do i get the corresponding file name and the file together for insertion
Since the text field will be stored in $_POST variable and file in $_FILES
First your form needs to have the attribute enctype="multipart/form-data" like so:
<form type="post" enctype="multipart/form-data">
On submit: you can get both $_FILES and $_POST arrays.
To see what you have, try (if submitted):
echo '<pre>';
print_r($_POST);
print_r($_FILES);
echo '</pre>';
If your file field name attribute was "upload_pic", then your $_FILES array should look like:
Array
(
[upload_pic] => Array
(
[name] => name_of_file
[type] => file_type
[tmp_name] => temporary_name
[error] => 4
[size] => 0
)
)
I believe that gives you any info you need to insert to DB. (E.g. $filename = $_FILES['upload_pic']['name']), add it to the $_POST array and carry on with your insert query.
EDIT:
Re-reading your question, I see:
When a user clicks on add a file. Two input fields are appended. One
with the input name of the file to be uploaded and another with input
field for the file itself...
I was wondering why you need to manually construct a separate text input for the "fileName", since you can obtain it from the $_FILES array (as explained above).
Store what is in $_POST and what is in $_FILES in variables. then you need to open de file with fopen and fwrite the to the file the variable you retrieved from $_POST. Don't forget fclose the file when you are done.
Is there a way to add a base64 image string to the $_FILES array (the array that you get when you upload a file)? Basically, I want to fake a file upload from a base64 string. The base64 string comes from an email attachment. I don’t wish to use file_get_contents(), because I want to save the image in a database as a binary image. I don’t need to upload it, as such, but I do need it be a part of the $_FILES array.
Uploading is a way of sending the contents of a file from a client (usually a web browser) to a server. It sounds like you've already got the data you need on the server (in PHP), so you don't need to upload it.
You can use base64_decode() to convert it from base64 to the straight file contents.
If you need it as a file on your server, you can use something like file_put_contents() to save it as a new file.
If you're trying to do something else with the uploaded file in the $_FILE array, please provide more details. Whatever it is, you don't need to actually upload the file.
The $_FILES array doesn't actually contain the contents of the file - it contains a filename where the contents have been saved.
If you must use the $_FILES array (in order to re-use an existing class, for example), you'll need to use file_put_contents() to save a new temporary file, and then manually add an entry the the $_FILES array pointing to that temporary file (Just like modifying any other array). Don't forget to delete your temporary file when you're done - this is normally handled automatically by PHP, but if you create the file manually, you'll have to delete it manually too.
I'd highly recommend refactoring or providing an alternate interface to your class instead though. You shouldn't have to pretend that the file was uploaded just to save it to the database.
index.php
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
/* Simple */
function previewImage( image, preview, string )
{
var fileImage = image.files[0];
var preview = document.querySelector( preview );
var reader = new FileReader();
reader.addEventListener( "load", function() {
preview.style.height = "100";
preview.title = fileImage.name;
// convert image file to base64 string
preview.src = this.result;
/* --- */
document.querySelector( string ).value = this.result;
}, false );
if ( fileImage )
{
reader.readAsDataURL( fileImage );
}
}
document.querySelector( "#imageID" ).addEventListener( "change", function() {
previewImage( this, "#imagePreviewID", "#imageStringID" );
} )
/* Simple || */
<form method="post" action="process.php" >
Title: <input type="text" name="title" /><br />
File Upload: <input type="file" name="image" id="imageID" /><br />
Preview: <img src="#" id="imagePreviewID" /><br />
String base64: <textarea name="imageString" id="imageStringID" rows="10" cols="50" readonly="readonly" ></textarea>
<hr />
<input type="submit" name="submit" value="Submit" />
</form>
process.php
<?php
$data = array(
"post" => $_POST,
"files" => $_FILES
);
echo "<pre>";
// echo json_encode($data);
var_dump($data);
?>
<img src="<?= $_POST['imageString']; ?>" />
repl.it
I have a html form that allows image uploads, but the image uploaded now fails the "is_uploaded_file" check for some reason.
So why does the upload fail the is_uploaded_file check?
HTML:
<form enctype="multipart/form-data" id="RecipeAddForm" method="post"
action="/recipes/add" accept-charset="utf-8">
<!--- Omitted Markup -->
<input type="file" name="data[Recipe][image]" value="" id="RecipeImage" />
<!--- Omitted Markup -->
</form>
PHP:
// returns false
echo is_uploaded_file($file['tmp_name'])?'true':'false';
I did a dump on the $file or $_FILES array:
Array
(
[name] => add or remove.jpg
[type] => image/jpeg
[tmp_name] => E:\\xampp\\tmp\\phpB9CB.tmp
[error] => 0
[size] => 71869
)
File size is not too large, and the error was 0. So why does it fail the is_uploaded_file check?
Might be a problem with windows, since it's case sensitive and will not match if the path is different. Try using realpath($file['tmp_name'])
try with
if (is_uploaded_file($_FILES['data[Recipe][image]']['tmp_name']))
remember $_FILES is reserve PHP superglobal variable ..so always write in capital
u can retrieve the correct path of file using
$filename = basename($_FILES['userfile']['name']);
NOTE: why u using array in the name attribute ( name="data[Recipe][image]" ) ?
if there is no specific reason then alwayz make simple code
<input type="file" name="RecipeImage" value="" id="RecipeImage" />
and check simply
if (is_uploaded_file($_FILES['RecipeImage']['tmp_name']))
Remember KISS
After you have used the uploaded image (move_uploaded_file) the function is_uploaded_file always returns false.
I am having a tough time trying to upload files through PHP.
My form:
<form action="blah.php" enctype="multipart/form-data" method="post">
<p> Upload file: <input type="file" name="xmlfile"/>
<input type="submit" name="upload_submit" value="Upload" /> </p>
</form>
Checklist:
No 'smart' quotes in sight. Fine.
Proper enctype. Fine.
name attrib in input tag. Fine.
My /tmp directory has the following permissions: drwxrwxrwt. Fine.
post_max_size = 50M, upload_max_filesize = 50M, file_uploads = On. Fine.
print_r($_FILES) gives Array(). Useless. Tried on images, xml files, etc. Nothing works.
What I don't understand even further is that there are pages on which file uploading works on the same server. The only thing different from what I can gather is that the page I am working on has a few other forms which aren't of enctype="multipart/form-data". Should this matter?
PHP code as requested:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['upload_submit'])){
print_r($_FILES);
exit();
...
}
}
Gives an empty array regardless of print_r's position; I also tried it right after the if($_SERVER['REQUEST_METHOD'] == 'POST'){
Are you sure that you are submitting the right form or you are dealing with the submitted data in the right place/script? Provide some of the PHP code please.
your from, this php script (as blah.php)
edit for debugging, dump $_POST if is false
error_reporting(E_STRICT);
ini_set('display_errors', 'on');
if(array_key_exists('xmlfile', $_FILES)) {
echo 'file ' , $_FILES['xmlfile']['name'] , ' recieved';
echo '<pre>'
, print_r($_POST, true)
, print_r($_FILES, true)
, '</pre>';
} else {
echo '<pre>'
, print_r($_POST, true)
, '</pre>';
}
(possible) output:
file rapid-gui-development-with-qtruby_p1_4.mobi recieved
Array
(
[upload_submit] => Upload
)
Array
(
[xmlfile] => Array
(
[name] => rapid-gui-development-with-qtruby_p1_4.mobi
[type] => application/octet-stream
[tmp_name] => /private/tmp/phpEyV3vy
[error] => 0
[size] => 556846
)
)
You mention having other forms on the page... are those properly closed? For instance, if you have:
<form method="post">
blah blah blah
<input type="submit" />
<!-- oops forgot to </form> here -->
<form method="post" enctype="multipart/form-data">
...
<input type="submit" />
</form>
Then the FIRST <form> tag could be taking precendence and submitting without the enctype set.
If you're on Firefox, I'd suggest using Firebug/HTTPFox/LiveHTTPHeaders (all available in FF's add-ons library) to see what's being sent over the wire, and running your page through a validator to make sure there's no goofy HTML bugs causing this.