Cloudinary upload php failed, how to fix it? - php

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

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

How i can receive the name of an image searched in html file object

Image of File Object: http://imgur.com/8USsHud
HTML of File Object:
<input type="file" name="file" id="file">
I want to receive the name of the image that i search in file object. In that image i want to receive Practidose.fw.png
I try to do this:
$imagem = mysql_escape_string($_POST['file']);
And i receive this error:
Undefined index on line 113
And i dont receive the name of the image
You should use $_FILE to get your file instead of $_POST. Check this link for more information.
$filename = $_FILE['file']['name'];
// name of the image is now in the variable $filename
use the correct file array of php.
Use this
$imagem = mysql_escape_string($_POST['file']['name'); instead of
$imagem = mysql_escape_string($_POST['file']);

Input file in wideimage

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.)

Categories