Input file in wideimage - php

I have these codes. In form.html:
<form name="form2" method="post" action="lagarto.php" />
<input type="file" size="32" name="myimg" value="" />
<input type="submit" name="Submit" value="upload" />
</form>
and in lagarto.php:
<?php
require( "./lib/WideImage.php");
$myimg = $_FILES['myimg']['tmp_name']; // <-- Note this addition
$image = WideImage::load($myimg);
$image = WideImage::load($img);
$image->output('jpg', 90);
?>
It should show the image output but I got this error message:
Notice: Undefined index: myimg in C:\Arquivos de
programas\EasyPHP-5.3.9\www\wideimage-11.02.19-full\lagarto.php on
line 6
Fatal error: Uncaught exception
'WideImage_InvalidImageSourceException' with message 'String doesn't
contain image data.' in C:\Arquivos de
programas\EasyPHP-5.3.9\www\wideimage-11.02.19-full\lib\WideImage.php:240
Stack trace: #0 [internal function]: WideImage::loadFromString(NULL)
#1 C:\Arquivos de programas\EasyPHP-5.3.9\www\wideimage-11.02.19-full\lib\WideImage.php(184):
call_user_func(Array, NULL) #2 C:\Arquivos de
programas\EasyPHP-5.3.9\www\wideimage-11.02.19-full\lagarto.php(8):
WideImage::load(NULL) #3 {main} thrown in C:\Arquivos de
programas\EasyPHP-5.3.9\www\wideimage-11.02.19-full\lib\WideImage.php
on line 240
Any idea?
EDIT:
I tried to add these lines to my code:
$_FILES = $HTTP_POST_FILES;
$img = $FILES['myimg']['tmp_name'];
I tried even only one of the lines. And nothing changed.

$myimg isn't automatically set based on the form, and you never declared it.
You'll need something like to define $myimg:
$myimg = $_FILES['myimg']['tmp_name']; // <-- Note this addition
$image = WideImage::load($myimg);

I remember I had same problem. The solution was copying the image form system temp folder to local one and then access it via loader. (Another reason can be bad file suffix in temporary name.)

Related

I'm having a php error can someone provide some solutions

I am having an error in my PHP code and I need help. If anyone knows the solution to this issue, please help me. The error message I am seeing is
[Fatal error: Uncaught BadMethodCallException: Method createtemplate is not defined. in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php:148 Stack trace: #0 C:\xampp\htdocs\php\index1.php(10): PhpOffice\PhpWord\PhpWord->__call('createtemplate', Array) #1 {main} thrown in C:\xampp\htdocs\php\vendor\phpoffice\phpword\src\PhpWord\PhpWord.php on line 148].
This is my code
<!-- HTML form for selecting the Word document to convert -->
<form method="post" enctype="multipart/form-data">
<label for="word-file">Select Word document:</label>
<input type="file" name="word-file" id="word-file">
<input type="submit" value="Convert">
</form>
<?php
// Check if the form has been submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get the uploaded Word document
$wordFile = $_FILES['word-file']['tmp_name'];
// Include the PHPWord library
require_once 'vendor/autoload.php';
// Create a new PHPWord object
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
// Load the Word document
$document = $PHPWord->loadTemplate($wordFile);
// Save the document as a PDF
$document->saveAs('converted.pdf');
// Show a message to the user
echo '<p>The Word document has been converted to PDF.</p>';
}
?>
have tried many solutions but nothing works....
I think you can use save method not to loadTemplate...
please replace your code...
From:
$document = $PHPWord->loadTemplate($wordFile);
$document->saveAs('converted.pdf');
To:
$document = $PHPWord->save($wordFile);
Through the log error, I think you didn't clarify method createtemplate in your PhpOffice\PhpWord\PhpWord.php file!
So it should be worked if you declare that method!

PHP Form Upload Error When Input File Field Is Empty

Here is the code for a form that will recreate the issue:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" ) {
print_r($_FILES['fileToUpload']);
if (!file_exists($_FILES['fileToUpload']['tmp_name']) || !is_uploaded_file($_FILES['fileToUpload']['tmp_name']))
$primaryImage = file_get_contents($_FILES['fileToUpload']['tmp_name']);
}
?>
<form method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image">
</form>
When clicking "Upload Image" with no file uploaded, PHP 8 will create this error (I've included the print_r($_FILES['fileToUpload']) output for reference).
Array ( [name] => [full_path] => [type] => [tmp_name] => [error] => 4 [size] => 0 )
Fatal error: Uncaught ValueError: Path cannot be empty in C:\xampp\htdocs\cole\cms\phpExample.php:6 Stack trace: #0 C:\xampp\htdocs\cole\cms\phpExample.php(6): file_get_contents('') #1 {main} thrown in C:\xampp\htdocs\cole\cms\phpExample.php on line 6
I have tried to wrap the issue in a try...catch... block, various checks like the if statement in the above to check for the emptiness of the path. Note, if you upload a file in the form and then click submit, no error will occur.
How can I prevent an error from being thrown while checking the presence of the $_FILE['my_file'] information in PHP 8?
You can check with:
if($_FILES['fileToUpload']['size'] > 0){
// code here
}
which will ensure that you have submitted a file, and its size is not 0

Image converting in PHP

I have a book where is a sample for creating a whole new picture, from a random picture given by the user, and I think there is something wrong in the example, at least I can't use it. The error code gave back by the PHP in the browser:
Fatal error: Uncaught Error: Call to undefined function imagecreatefromstring() in C:\xampp\htdocs\kepkonvertalas.php:13 Stack trace: #0 {main} thrown in C:\xampp\htdocs\kepkonvertalas.php on line 13
The HTML form:
<form action="kepkonvertalas.php" method="post" enctype="multipart/form-data">
<input type="file" name="kep"><br>
<input type="submit" value="kuldes">
</form>
The PHP part:
<?php
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);
$forraskep = imagecreatefromstring($kep);
$szelesseg = imageSX($forraskep);
$magassag = imageSY($forraskep);
$ujmagassag = 400;
$ujszelesseg = $ujmagassag*($szelesseg/$magassag);
$ujkep = imagecreatetruecolor($ujszelesseg, $ujmagassag);
$eredmeny = imagecopyresampled($ujkep, $forraskep, 0, 0, 0, 0, $ujszelesseg, $ujmagassag, $szelesseg, $magassag);
imagejpeg($ujkep, "ujkep.jpg");
?>
<img src="ujkep.jpg" alt="">`
What is suspect, is that this extension is not implemented into my php.ini, but I cant find any proof for it, either solve the problem. Thanks for the help!
I tried to implement a picture from an URL, and do the conversion on that, which means I deleted the
$fajl = $_FILES['kep']['tmp_name'];
$kep = file_get_contents($fajl);`
And I've made a variable with a picture URL, and in the
$forraskep = imagecreatefromstring($kep);
I just changed like this:
$forraskep = imagecreatefromstring(file_get_contents($src);
Well the next problem was, that the $forraskep variable was not defined.
I expect an output where I have the picture with the size of the given parameters.
The solution was actually kinda easy:
open php.ini
search for "extension=gd"
delete the semicolon
restart apache server

Cloudinary upload php failed, how to fix it?

I try using cloudinary for upload my image and video for my personal blog but it's a fail. I watch this video tutorial https://www.youtube.com/watch?v=oZCQLjfq97o. the error says :
Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 8
Notice: Undefined index: file in >C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php on line 9
Fatal error: Uncaught Cloudinary\Error: Missing required parameter - file in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php:558 Stack trace: #0 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(407): Cloudinary\Uploader::call_api('upload', Array, Array, NULL) #1 C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php(100): Cloudinary\Uploader::call_cacheable_api('upload', Array, Array, NULL) #2 C:\xampp7\htdocs\lovantoBlog\cloudinary\index.php(11): Cloudinary\Uploader::upload(NULL, Array) #3 {main} thrown in C:\xampp7\htdocs\lovantoBlog\cloudinary\vendor\cloudinary\cloudinary_php\src\Uploader.php on line 558
This is my code for upload :
require 'vendor/autoload.php';
require 'config.php';
if (isset($_POST['Simpan'])) {
$nama = $_POST['nama'];
$slug = $_POST['slug'];
$gambar = $_FILES['file']['name'];
$file_tmp = $_FILES['file']['tmp_name'];
\Cloudinary\Uploader::upload($file_tmp, array('public_id' => $slug));
}
and this is the code for form :
<form method="POST">
<input type="text" name="nama">
<input type="text" name="slug">
<?php echo cl_image_upload_tag('image_id');?>
<input type="submit" name="Simpan" value="Simpan">
</form>
The cl_image_upload_tag is used to perform direct uploading from the browser to Cloudinary. When you submit the form you there is no file parameter sent to your backend and thus the Cloudinary::Uploader::upload call fails with the error Missing required parameter - file.
If you would like to upload the file from your backend then you need to replace the cl_image_upload_tag with a regular file input field element. Once a file is selected and the form submitted your backend can use the same code to upload the file to Cloudinary.
On the other hand, if would like to use cl_image_upload_tag then please see the direct uploading section of the Cloudinary documentation that describes setting this up.
https://cloudinary.com/documentation/php_image_and_video_upload#direct_upload_file_tag

How to display divs after checking password with an HTML form

I'm programming a simple password checker with a html form, where you input the code and this code is compared as hash to another hashed code in the database. If the password matches, the index.php file shows a few divs, which otherwise are completely hidden from the user.
I've tried things like:
$input = "";
<center>
<form action="" method="GET">
CODE:<br>
<input type="text" name="code" value="">
<br><br>
<input type="submit" value="Submit">
</form>
</center>
<?php
$checker = new Checker();
$input = $_GET['code'];
//echo "<style>.Main_Content{ visibility:hidden;}</style>";
if ($checker->compareWords($input, 'Beginners')) {
echo "success Beginners<br>";
include 'Beginners_Video.php';
echo "<style>.Main_Content{ visibility:show;}</style>";
}
else {
echo "false Beginners<br>";
}
?>
Fatal error: Uncaught TypeError: Argument 1 passed to
Checker::compareWords() must be of the type string, null given, called
in D:\www\www91\members\members.php on line 64 and defined in
D:\www\www91\members\checker.php:23 Stack trace: #0
D:\www\www91\members\members.php(64): Checker->compareWords(NULL,
'Beginners') #1 {main} thrown in D:\www\www91\members\checker.php on
line 23
The result should be no error and just the whole thing hidden, or the output "False Beginners. Nothing else"
I know that i'm just stupid and that the answer is probably really simple. I've tried and I can't figure it out.
Thanks for your help.
Your code should work once you filled in and submitted the form, but there is no check handling the case where $_GET['code'] is unset or empty. Change your if statement to
if (!empty($input) && $checker->compareWords($input, 'Beginners')) {
in order to check for this first. If !empty($input) is false, the next condition won't be checked at all, so there won't be an error message and it will jump to the else clause directly.
On a side note: You should think about whether GET is the right method for what you're trying to achieve or whether POST would be better. With GET, The codes users enter might get saved in the browser's history and be visible to others using the same computer. Also, for passwords use type="password" for the input instead of displaying it in cleartext.
And one more thing: the <center> tag is deprecated and you should use CSS instead for layout.
At the very beginning you should check if something is passed in code parameter, using the isset() method. If $_GET['code'] is empty it will return null and this is why you get must be of the type string, null given.
<?php
if(isset($_GET['code']) {
$input = $_GET['code'];
} else {
// 'code' parameter is empty
$input = '';
}
$checker = new Checker();
// ...

Categories