so i decided against using uploadify as i could not get any help on it. i've been now trying to implement a simpler solution. but unfortunately i cannot seem to process the upload code on a single page without receiving the following error:
Warning: fopen() [function.fopen]:
Filename cannot be empty in
C:\xampp\htdocs\speedycms\manageclient.php
on line 205
Warning: fread() expects parameter 1
to be resource, boolean given in
C:\xampp\htdocs\speedycms\manageclient.php
on line 206
Warning: fclose() expects parameter 1
to be resource, boolean given in
C:\xampp\htdocs\speedycms\manageclient.php
on line 208 Error, query failed
this is the code i've been trying to get to work... can you suggest any means of getting it all to work on a single page rather than splitting it up into two apart from ajax (im not too familiar with that)... its all on a single page
any help would be appreciated!
<!-- upload file --->
<?php
if (array_key_exists('uploadfile',$_POST)) {
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$uploadQry = "INSERT INTO upload (name, size, type, content ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";
mysql_query($uploadQry) or die('Error, query failed');
echo "<br>File $fileName uploaded<br>";
exit;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] . '?id=' . intval($client_id); ?>">
<p>
<b>File Upload</b></p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile"><BR />
<input type="hidden" name="uploadfile" value="1"/>
<input name="upload" type="submit" id="upload" value=" Upload ">
</p>
<p> View files
</p>
<div id="viewFiles" style="display:none;" rel="facebox">
<div style="width:300px; height: 300px;"></div></div></form>
You need to set the correct encoding of your form to enable file uploads:
<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
...
</form>
Edit: Filename cannot be empty is telling you that you have not specified the file name in your first call to fopen - you're passing in $tmpName to that method, which is assigned from $_FILES['userfile']['tmp_name'].
The only reason I can think of for $_FILES['userfile']['tmp_name'] to be empty is that you haven't actually uploaded a file?
The error message says that in
$fp = fopen($tmpName, 'r');
$tmpName is blank (or NULL).
Try looking in $_FILES to see what you've got:
print serialize($_FILES);
Related
So, I'm trying to upload an image using move_uploaded_file(). Here is the code:
HTML FORM:
<form method="post" action="?page=prod&action=register" enctype="multipart/form-data">
<fieldset class="field-prods">
<label>Name:</label> <br>
<input type="text" name="txtName">
</fieldset>
<fieldset class="field-prods">
<label>Price:</label> <br>
<input type="number" name="nmbPrice">
</fieldset>
<fieldset class="field-prods">
<label>Image:</label> <br>
<input type="file" name="fileImage">
</fieldset>
<button type="submit" class="btn-prods">Register</button>
</form>
PHP SCRIPT:
if ($_POST != null) {
if (!empty($_POST['txtName']) && !empty($_FILES['fileImage']) && !empty($_POST['nmbPrice'])) {
Product::insert($_POST['txtName'], $_FILES['fileImage'], $_POST['nmbPrice']);
$target = "img/prods/" . $_FILES['fileImage']['name'];
$fileTmpName = $_FILES['fileImage']['tmp_name'];
move_uploaded_file($fileTmpName, $target);
}
}
But I'm getting lot of warnings:
*Warning: Array to string conversion*
*Warning: move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: No such file or directory*
*Warning: move_uploaded_file(): Unable to move "F:\Programs\Xampp\tmp\php7CE5.tmp" to "img/prods/livraria-print1.jpg"*
Can someone please help me with that?
# Array to string conversion
This warning is in insert function: you passed array ($_FILES['fileImage']) instead of string.
It seems that you want to store address of image, if that so it is better to store the image first then try to save the address of image.
# move_uploaded_file(img/prods/livraria-print1.jpg): Failed to open stream: ...
This warning is clear. check your directories and sure that the path you used is correct.
# move_uploaded_file(): Unable to move
And this one is because of prev warning.
I have a C program running on the localhost server which generates some files( writes its output to these file). Then I zip all these files together using php. Now I want to upload the zip file produced to mysql server using some php script but i dont want the user to upload these files using a form by clicking choose file button but i would like to upload these automatically as soon as they are generated to mysql using php script.
<?php
//get content of json file
require_once("zip.php");
$str = file_get_contents("program/heatmap_parameters.json");
//decode the JSON
$json = json_decode($str,true);
//echo '<pre>' . print_r($json, true) . '</pre>';
foreach ($json['parameters'] as $params) {
echo $params[3]."<br>";
$files_to_zip = array("program/".$params[3]);
var_dump($files_to_zip)."<br>";
//$files_to_zip = array("program/cctv2.mp4.json");
//if true, good; if false, zip creation failed
$zip_name = "program/".$params[3].".zip";
//zip the files
$result = create_zip($files_to_zip,$zip_name);
}
//connect to database
require_once("database/conn.php");
//upload zip file to database without displaying any form.
?>
I need a way to upload my generated file to saved to mysql without choosing it from the form.
<form action="" method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
include '../database/conn.php';
$sql = "INSERT INTO upload (name, content ) ".
"VALUES ('$fileName', '$content')";
$query = mysqli_query($conn,$sql);
if(!$query){
die("Cannot Insert".mysql_error());
}
}
How can i make this code such that it does not need the form
To answer your question in oneline,
You can not upload a file without form/input type file element, because that will a big security hole to web.
To upload any file to server, server need files object which has all the info of file. that info will be given by client from where you are uploading a file. It will also has the tmp location of file form where server will take that file.
You can copy file from one server to another, using curl or you can
write a bash script which will copy file from one place to another and
you can run that bash script using PHP.
Hope this helps to you!
I want to convert a image to Base64 and put in into my database.
I know how to encode a image to Base64, but I dont know how to use a file from a user.
So the user can "upload" a file with <input type="file" />
But how can I approach that file without download the file and store it at my server?
So i encode the file actually localy on the user his/her computer
Thanks
The BLOB datatype is best for storing files.
This PHP sample goes over it pretty well
Also see: How to store .pdf files into MySQL as BLOBs using PHP?
The MySQL BLOB reference manual has some interesting comments
if you do not want to save the file to the database, but just read it, use:
<form method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
$content here has the file content
To encode an image to base64 you can use file_get_contents to read the image file:
$imageBinaryString = file_get_contents('/image/file/path.png');
http://us1.php.net/file_get_contents
Then base64 encode:
$base64_imageString = base64_encode($imageBinaryString);
You can then store in database in a column with type text or varchar.
<input type="file" name="icon" />
Now try with below code.
$base = $_REQUEST['icon'];
$binary = base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
$file = fopen('upload/imagename.png', 'wb');
$imagename = 'path_to_image_/images.png';
fwrite($file, $binary);
fclose($file);
htm code :
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
Movie :<br />
<input name="fileField" type="file" size="30" /><br />
<input name="submit" type="submit" class="submit" value="Send" />
</form>
php code :
if ($_FILES['fileField']['tmp_name'] !=""){
$fileName = $_FILES["fileField"]["name"];
$fileType = $_FILES["fileField"]["type"];
$fileTmpLoc = $_FILES["fileField"]["tmp_name"];
$fileName = preg_replace('#[^a-z.0-9]#i', '', $fileName);
move_uploaded_file($fileTmpLoc, '../upload/video.flv');
}
this code not work for VIDEO file but work correct for other file ( jpeg , mp3 , png and ,,, )
Perhaps change your directory to
move_uploaded_file($fileTmpLoc, '../upload/ ');
this problem from php setting
php set to Max 2MB size for upload
You have absolutely no error handling on the code, meaning you have no way of telling when an upload failed. Add:
if ($_FILES['fileField']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error " . $_FILES['fileField']['error']);
}
as some bare-minimum error handling. The error codes are defined here: http://php.net/manual/en/features.file-upload.errors.php. Checking for the absence of a 'tmp_name' is NOT a proper check.
I am building a php application.
I can easily upload an image or any other type of data, but not a .jar. Below my code:
Upload.php
<form action="getfile.php" method="post" name="uploadForm" id="uploadForm" enctype="multipart/form-data" ><br>
<?php echo gettext("Image "); ?>
<input name="imagen" value="" type="file" id="imagen" />
<?php echo gettext("Jar "); ?>
<input name="jarFile" value="" type="file" id="jarFile" />
</form>
getfile.php
$fileName = $_FILES['jar']['name'];
$fileType = $_FILES['jar']['type'];
//Check the extension
if (!strpos($fileType, "jar") ) {
echo gettext("The simulation must be jar extension. Try again.");
}else{
if (move_uploaded_file($_FILES['jar']['tmp_name'], $path)){
echo gettext("Simulation stored succesfully.");
}else{
echo gettext("Something happened. Try again. ");
}
}
For images I am following the same aproach, but when I try to upload a jar file, I am always getting the error
The simulation must be jar extension. Try again
and $fileType is empty. Is there some restriction at this point? Am I missing something??
Thanks in advance
The type property is unreliable (and populated by the browser). Your best bet is to retrieve the MIME file type:
$fhandle = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($fhandle, $_FILES['jar']['tmp_name']);
The $mime_type should be application/java-archive.
PHP >= 5.3.0