i need your help...i want to copy/upload images on server and saving directories in database...Problem is the images are empty =(
I'm using:
WampServer
Apache Version : 2.2.22
PHP Version : 5.3.13
MySQL Version : 5.5.24
Issue is that it's creating the file but is empty.... can it be WampServer fault?
CODE:
<input type="hidden" name="MAX_FILE_SIZE" value="30000">
<input name="image" id="image" multiple="true" type="file" />
<input name="name" id="name" type="text" maxlength="50" value="" placeholder="Enter Image Name" class="text-field"/>
if(isset($_POST['name']) && isset($_POST['image']))
{
$name = $_POST['name'];
$img = $_POST['image'];
$file_url = $img;
$fp = fopen($file_url, 'rb');
$content = fread($fp, filesize($file_url));
$fp = fopen('../Images/UploadedImages/'.$img, 'wb');
$image='../Images/UploadedImages/'.$img;
$result=mysql_query("insert into Images(Name,Directory,Register_Day)
values ('$name','$image',now())");
if (!$result) {
die("Failed to load");}
else{
fputs($fp, $content);
fclose($fp);
}
Uloading images doesn't work like that, you must do something like this:
$img = $_FILES['image']['tmp_name'];
Also note that you don't want to save the image into the database, you (probably) want to save the image to the server but you save only the file location into the database.
You cant upload image by using fopen. Try below code.
Your will get image details in $_FILES array not in $_POST.
$tmp_name = $_FILES['image']['tmp_name'];
$name = $_FILES['image']["name"];
$uploads_dir = '../Images/UploadedImages';
move_uploaded_file($tmp_name, "$uploads_dir/$name");
In above code $uploads_dir/$name will be your image path which you can store in db.
And also make sure that your <form> tag has enctype="multipart/form-data" attribute set.
Related
i have a php form and use html2pdf (TCPDF). The php form works when I use text and images that already include in the form, but now I want to attach a image and also include that in the pdf file.
The code I am using now I get the filename.
To attach the image I am using
$<span><input type="file" name="foto"></span>
And to include it in the pdf-file I use this code
$foto = $_POST['foto'];
$content .= '<img src='.$foto.'with=200 height=auto>';
Sorry for my bad English!
Regards
Daniel
Do an normal HTML file upload like this:
<form method="post" enctype="multipart/form-data">
<label>Deine Datei ;) :
<input name="fileUpload" type="file" accept="image/*">
</label>
<label>Your Name:
<input name="userName" type="text" accept="image/*">
</label>
<button type="submite" name="submite">Upload</button>
</form>
And on the PHP side you must move your file:
if(isset($_POST["submit"])) {
$userName = $_POST['userName']; // the normale methode
$check = getimagesize($_FILES["fileUpload"]["tmp_name"]);
if($check !== false) {
$temp_path_to_file = $_FILES['fileUpload']['tmp_name'];
$file_content = file_get_contents($temp_path_to_file);
// and now past your image to your PDF.
// if you want to save the image on the server you must move it with this (its not necessary if you want only to past it in your PDF)
if(move_uploaded_file($temp_path_to_file, '/my/image/folder/image_'.uniqid() . '.' . pathinfo($filename, PATHINFO_EXTENSION);
}
}
EDIT:
For your example you must convert the $file_content to an base64 string. Than you can past it in your image:
$base64 = 'data:image/' . pathinfo($filename, PATHINFO_EXTENSION) . ';base64,' . base64_encode($file_content);
echo '<img src="'. $base64 .'" style="with: 200; height: auto">';
I am trying to upload the file inside wordpress folder (wp-content/themes/twenty-twelve/uploads/) it is failing everytime when I submit the form.
My form path (wp-content/themes/twenty-twelve/template-parts/form.php)
<form id="Form" method="post" action="/checkout/" enctype='multipart/form-data'>
<input type="file" name="image">
<input type="submit" name="submit" value="Proceed to Payment" id="FormSubmit" />
</form>
Checkout.php path(wp-content/themes/twenty-twelve/template-parts/checkout.php)
if(isset($_POST['submit'])){
$Image = $_FILES['image']['name'];
$Image = implode(",",$_FILES['image']['name']);
$thisFile = $_FILES['image'];
$Image = $_FILES['image']['name'];
$FileTmp = $thisFile['tmp_name'][0];
$sFileTypeArr= explode(".",$Image );
$sFileType = end($sFileTypeArr);
$randNo = date("ydhis");
$ImageFinal = "IMG_".$randNo.".".$sFileType;
$url = '/wp-content/themes/twenty-twelve';
$fileSavePath = $url."/uploads/";
$ImageFinalWithPath = $fileSavePath.$ImageFinal;
$upload_file = move_uploaded_file($_FILES['image']['tmp_name'], $ImageFinalWithPath);
var_dump($upload_file);
}
Error: bool(false)
NOTE: I have checked the file permission, given full permission. I have also checked the max_file_size_upload and other setting of php.ini.
I am trying to upload one file at a time. It shows the error bool(false)
You should use WordPress helpers
wp_upload_dir()
Get an array containing the current upload directory’s path and url.
See wp_upload_dir() and Determining Plugin and Content Directories.
I am new to php and is it possible to rename an image file before uploading to the database?
EDIT: would be using a form to upload the file to a database.
<input type="file" name="image">
<input type="submit" name="upload" value="Add" action="viewpage.php">
EDIT: IMAGE OF DATABASE:
The second image above still shows the original image file name in the database while the image name in the directory already changed.
Yor can try this..
<?php
if(isset($_POST['submit_btn']))
{
$tmp_file = $_FILES['uploadedfile']['tmp_name'];
$ext = pathinfo($_FILES["uploadedfile"]["name"], PATHINFO_EXTENSION);
$rand = md5(uniqid().rand());
$post_image = $rand.".".$ext;
move_uploaded_file($tmp_file,"../post_imgs/".$post_image);
}
?>
I am having some difficulty uploading an image to a folder on my web server. Here's my code:
HTML:
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image">
<input type="submit" value="Upload Image" name="upload">
</form>
PHP:
<?php
if (isset($_POST["upload"])) {
$name = $_FILES["image"]["name"];
$type = $_FILES["image"]["type"];
$size = $_FILES["image"]["size"];
$temp = $_FILES["image"]["tmp_name"];
$error = $_FILES["image"]["error"];
$target_file = "/profiles/images/$name";
move_uploaded_file($temp, $target_file);
}
?>
I've tried echoing out the file name ($name), but it doesn't return anything at all. It's blank for some reason. This is when I try to upload an image. When I echo $target_file, I get this "/profiles/images/", the $name part is not included for some reason.
Check your max upload file size. If in the settings the size is smaller than the file you are trying to upload, nothing is send.
This is happens in those cases when we try to upload very heavy size image, Please try to check your maximum file size, and please for testing try to upload maximum 100kb size of pic through this code. Hope this will work.
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