Upload image and retrieving in server WORDPRESS - php

I'm trying to do uploading of image in the server and retrieving. I have done something but the problem is that the image is not uploading. My script will only get the file name and then the image will be save on the server folder. This script only saves the other data but not the image, I'm not sure where is the mistake, but the path I've used is corret, also I need to retrieve the image and post it on front end.
<div class="wrap">
<form method="POST">
Date: <input type="text" name="class_date"> <br>
Event: <input type="text" name="class_name"><br>
<input type="hidden" value="class_sched" name="posttitle"><br>
<input type="hidden" name="size" value="350000">
Image:<input type="file" name="class_image"><br>
<input type="submit" value="submit" name="submit">
</form>
</div>
<?php
global $wpdb;
if(isset($_POST['submit']))
{
$target = plugins_url("/images",__FILE__);
$target = $target . basename( $_FILES['class_image']['name']);
$class_date = $_POST['class_date'];
$class_name = $_POST['class_name'];
$class_image = ($_FILES['class_image']['name']);
$wpdb->insert('wp_calendar', array('class_name'=> $class_name, 'class_date'=>$class_date, 'class_image'=>$class_image));
//Writes the photo to the server
if(move_uploaded_file($_FILES['class_image']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['class_image']['name']). " has been uploaded, and your information has been added to the directory";
}
else
{
//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
}

Add move_uploaded_file function. In your above code only check move_uploaded_file is true or not. Because it is if statement.
Add following code after $wpdb->insert('wp_calendar', ....
move_uploaded_file($_FILES['class_image']['tmp_name'];
move_uploaded_file function will move file from temporary folder to plugin/images folder.

Related

How to send file to $_FILE through html form?

Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}

Form doesn't see file uploader - php

I'm attempting to build my first form/file uploader (I'm a newb fyi).
I am testing on a local server on my mac, both the form, file handler, and the uploads folder are in the same file directory with one another.
When I select a file using the submit form (test file is 'testFilego.txt' and 3 bytes in size'), i get the following error: http://localhost/PhP_exercises/__tizag/280-php-fileupload-test.php?MAX_FILE_SIZE=2500000&uploadedfile=testFilego.txt. The submit form doesn't seem to connect to the handler (?). I thought the test file would appear in my uploads folder. Help.
This is the submit form:
<form>
<form enctype="multipart/form-data" action="280-php-uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2500000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
</form>
This is the file hander the form ought to be contacting
<?php
280-php-uploader.php&lt?php
//This is '280-php-uploader.php'
// Where the file is going to be placed
$target_path = "uploads/";
// Add the original filename to our target path.
//Result is "uploads/filename.extension"
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
As Marco Acierno observed, i'd posted the form inside a form and that seems to have caused the problem.

PHP Upload Wont Work, But Wordpress Upload Does

I am creating my own upload script to upload files to my webserver (like small flash videos). However, it does not seem to be working. However, I don't think it is PHP because uploading files to my server via Wordpress does work.
Here is the upload form:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Video: <input name="vid" type="file" />
<br/>
Name: <input name="title" type="text" />
<br/>
Description:
<br/>
<br/>
<textarea name="desc">Your description here</textarea>
<br/>
<br/>
<input type="submit" value="Upload" />
</form>
Here is the upload PHP page:
<?php
$target = basename( $_FILES['uploaded']['name']) ;
$ok=1;
//check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
I have set the max upload size in php.ini to 100MB, however, the upload still fails. My php.ini file can be viewed here at pastebin. Also note that I have removed all verification from the upload script to see if it works.
From the looks of your code, you are uploading the file in the same directory as the running script. Are you sure you have write permission for the webserver in that directory?
Try uploading the file in your temp directory:
move_uploaded_file($_FILES['uploaded']['tmp_name'], sys_get_temp_dir().DIRECTORY_SEPARATOR.$target);
and see if it works
UPDATE
You are calling your input file "vid" but you are trying to access the input "uploaded".
replace all your $_FILES occurences with $_FILES['vid'][]...
And enable error reporting on your machine
How could you get the value of video from this:
$target = basename( $_FILES['uploaded']['name']) ;
WHile the 1st block contain the name of the file which was given to the html.
IN your case it is : Video: <input name="vid" type="file" /> Vid
SO use this on your code:
<?php
$target = basename( $_FILES['vid']['name']) ;

Image uploading via PHP returning empty array

I am unable to successfully upload an image/file to my server. The php is as follows:
//This is the directory where images will be saved
$uploadDir = "./";
$uploadFile = $uploadDir . basename( $_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $uploadFile)){
echo "The file has been uploaded successfully.";
} else {
print_r($_FILES);
}
I chose the directory at which this script lives, to ensure the functionality before I upload to the final directory. I want to upload photo's, and will check for file extensions later - but for now I at least need the upload functionality to work.
I get an empty array returned.
The form is as follows:
<form id="imageUploadForm" name="imageForm" enctype="multipart/form-data" action="imageController.php">
<label for="photo" class="blogLabel">Upload an Image</label>
<input type="file" name="photo" id="imageUpload">
<input type="submit" name="submit" id="imageSubmit" class="btn btn-primary" value="Upload">
</form>
You forgot the most important thing about a form -- the method!
If there is no method set it defaults to get.
You want post!
Add method="post" to your form.

trying to upload file to server, but php script not getting file details

I am trying to upload an image to a server, then get that image name and insert it into mysql database. Before I got to the database bit, the upload isn't working. I have narrowed it down to the fact there is no data in the $_FILES array, when I echo out $imageFileName is it blank.
Here is part of my form:
<form name="addItem" method="post" action="add-new-item.php">
<input name="name" placeholder="Portfolio Item Name" type="text" id="itemName"/><br />
<input type="file" name="imageName" id="imageName" /><br />
</form>
Script, which is part of the same page the form is on:
$target = "images/";
$target = $target . basename( $_FILES['imageName']['name']);
$imageFileName=($_FILES['imageName']['name']);
if(move_uploaded_file($_FILES['imageName']['tmp_name'], $target)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}else {
echo "Sorry, there was a problem uploading your file.";
}
$query1="INSERT INTO portfolio_items (item_name,full_size_image) VALUES('$itemName','$itemImage')";
You need to include the following in your <form> tag :
enctype="multipart/form-data"
So it would become :
<form name="addItem" method="post" action="add-new-item.php" enctype="multipart/form-data">
Documented very well here - see the note at the bottom of the first page ...

Categories