I have a small issue actually getting variable to show - let alone how to figure out why my images are not uploading.
On my form I have the following textarea -
<input type="file" name="imageNew" />
in the page to which this form is posted I have -
$imagetoshow = $_REQUEST['imageNew'];
echo "the image is " + $imagetoshow;
exit();
I simply cant see why this is displaying a "0"?
What I think I need is for someone to point out the obvious to me - obvious to you, not to me. Then I can get on with figuring out why the file isn't uploading.
Thanks in advance.
Files work a little differently... You need to access the $_FILES superglobal.
http://www.php.net/manual/en/features.file-upload.post-method.php
(From that page as well: Note: Be sure your file upload form has attribute enctype="multipart/form-data" otherwise the file upload will not work.)
To get files, you need to use $_FILES, not $_POST, and the form needs to have enctype="multipart/form-data". Check out the following link for more about file uploads in PHP: http://www.php.net/manual/en/features.file-upload.php
Related
It may sounds not clear, but I'll try.
I'm working on my edit page where I can edit several input types as well as file.
I'm saving my files in my database with its filename.
However, since input type = file doesn't allow to keep the data / value on it, unless I re-upload the file, it will just wipe out the filename on my database.
How would I be able to keep the filename as it is in my database without re-uploading the file?
It is very simple. You just don't need to update the database field that containing the file name if the input file is empty.
Here is a demo for you.
Lest's assume your form is like this.
<form>
<input type="file" name="myfile">
</form>
So you need to check this in your backend.
if(isset($_FILES['myfile']) && !empty($_FILES['myfile']['name']))
{
// update your databse column with new file detail
}
Hope this helps.
I am trying to write a form's content to a text file on my computer, which is from where the server is running, and I can successfully write variables that are defined like$newVar = "Text to write."; , but when I try to pass a value from a form in my HTML code by $newVar = $_POST["varOutput"]; , nothing will be written in the file. My HTML form appears as so:
<form id="hidden" action="submit.php" method="post">
<textarea name="varOutput" form="hidden" rows="73" cols="100" id="varholder"></textarea>
</form>
I have some JavaScript that puts a string into the text area, but the string is different for each user, as they will interact with the website differently. I have tried countless times to change every little detail in my code to make it work correctly, but my efforts have been fruitless. Any help would be appreciated.
EDIT:
The PHP code is
$output = $_POST["varOutput"];
$fp = fopen("text1.txt","a");
$savestring = ($output . ānā);
fwrite($fp,$savestring);
fclose($fp);
That is the entirety of the php file that the javascript references. The "n" is to test to see if anything happens. It prints every time.
Debug
1) check if the value is set in textarea.
You can use firebug or inspect hidden elements to know if value is set correctly.
2) print all posted data to on php to know if your data is posted to backend.
After hours of trying different ways, I found out that I could get the $_POST to work if I used an <input type="submit" /> element inside the form. This allowed the php variable to read the text inside the form and write it to my text file. Thanks for everyone's help, and I hope this will help someone else. Have a nice day.
The purpose is to allow user uploading files (usual uploading process) and confirm uploading by pressing on the confirmation button. Programming side: 2 folders - 1 for unconfirmed files where files get deleted periodically and 2 - confirmed folder - where files are copied from unconfirmed folder if a user presses the confirmation button.
Basically, I have a form, where a user uploads files that are stored in the folder and the path to it - in a database. And on the same page I have another button, so when the user presses it I would like to move his/her uploaded files from one directory to another (not re-uploading - coping on server).The issue is taht I should know WHICH files to deal with!
My problem is: everything seems ok, but I can't get the variable $name - that tells me the name of the file, user has uploaded. Because When I try to use it to proceed second submit button - it says variable is undefined. I need to get the variable that tells me the name of the file user has submitted so I can copy that file to another directory, but it can only be assigned (and unfornunatelly used) when procesing the form submittinf and enabled to use only inside of the processing, while I want to use it outside.
I have tried to use sessions - but this code doesnt work, same problem - it says that - 'undefined index - regex', same with COOKIES.
All the html:
<form action="videator.php"method="post" enctype="multipart/form-data">
<input class="form" type="file" name="fileToUpload" id="fileToUpload" accept="video/*" >
<input class="form" type="submit" value="Upload Image" name="submit">
</form>
<form action="love.php" method="post">
<input class="post" type="submit" value="POST" name="post" id="post"/>
</form>
videator.php:
if(isset($_POST["submit"])) {
$file = $_FILES['fileToUpload'];
$name = $file['name'];
$_COOKIE['name'] = $name;
$_SESSION['regex'] = $name;
...
}
love.php
if(isset($_POST["post"])) {
session_start();
$name = $_GET['regex'];
$from = "temp_videos/".$name;
$to = "videos/";
if (!copy($from, $to)) {
echo("<script>alert('fail')</script>");
}else {
echo("<script>alert('Success!')</script>");
}
It says also that 'The first argument to copy() function can not be a directory. I guess, that's due to the fact that my variable $name is undefined, therefore only the folder is here as the path.
Please, help. I've spent all my day on that.
(If I just use copy() function with indicating actual path and not a variable of it - everything works)
Thanx in advance.
I have a few tips that may help.
(1) At the top of each PHP file, as the first instruction, put:
<?php
if (!session_id()) session_start();
//any other PHP instructions follow.
?>
(2) Try using the excellent jQuery File Upload plugin by Ravi Kusuma. Although I generally try to code everything myself and not use plugins, this one is so well done that it's my primary goto for file uploads.
(3) Until you get this working, there is no need to put the session_start() inside an if statement. The session_start() must be the first instruction at top of each PHP page that references the $_SESSION variable.
(4) Look into AJAX instead of using <form> (for managing the file upload process). Kusuma's plugin also works great with AJAX. If you haven't used AJAX before, it may sound intimidating - but it's super simple. Copy the examples at the bottom of this post and make them work on your system. It may be the most important 15 mins you've spent in 2016. Note that the point of the linked question is that there must be a separate PHP file that receives the AJAX post.
(5) Don't forget that AJAX is a two-step process: (a) the javascript on the current page communicates with the specified PHP page; (b) the PHP page receives the data (or file upload data) via POST varibles. Therefore, be sure to check out the "SERVER SIDE" tab at top of Kusuma's Hayageek page. At the top of the SERVER SIDE page are four links to sample PHP files. Study the upload.php example.
Happy coding.
I'm using this image uploader:
http://www.jotform.org/jquery/ajax-multi-file-upload-with-progress-bar/#more-291
It's jQuery/Ajax, while I have it working I know little about either language. I want to modify it so that the filenames of all uploaded files get put into a database.
The included upload.php file doesn't appear to work as I've deleted that and the uploader is still functioning. If that was required then I could quite easily write the required PHP to do the job.
One solution would be for a PHP file to be executed after the upload finishes, how can I do that?
Appreciate any help here.
<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
<div id="drop">
Drop Here
<a>Browse</a>
<input type="file" name="upl" multiple />
</div>
<ul>
<!-- The file uploads will be shown here -->
</ul>
</form>
Edited above - this is the form that the upload uses, is there anyway I could use an "isset" to check it's been used, there's no submit button though.
The above code is what I've got on the page with the uploader on it, it doesn't seem to actually call the upload.php file.
I've not used this file uploader before. Having looked at it though, upload.php is responsible for processing the file after upload.
If you look at this script:
if(move_uploaded_file($_FILES['upl']['tmp_name'], 'uploads/'.$_FILES['upl']['name'])){
echo '{"status":"success"}';
exit;
}
You can see that on successful upload, it moves the file to the included uploads directory. It's at this point that you could extend this script and save $_FILES['upl']['name'] to a database. You might want to look in to the possible PHP database extensions
$('#upload').fileupload({
// This element will accept file drag/drop uploading
dropZone: $('#drop'),
// This function is called when a file is added to the queue;
{***SNIP***}
});
// Automatically upload the file once it is added to the queue
var jqXHR = data.submit();
},
The second to last line var jqXHR = data.submit(); is when it submits the form. That's the AJAX part of it. Since you deleted the upload.php file, it actually gives a 404. However, the code was poorly written and doesn't seem to alert you it cant reach said file.
Nonetheless, upload.php IS getting called, and IS being used. As someone else above suggested, you can extend that to get any details about the uploaded files you need. (And actually, I would put any logic before the echo. That echo is what tells the JavaScript everything is hunky-dorey.)
I have a form which takes a couple of text field inputs and then a folder path.
Now before I submit the form I want to make sure that whatever the folder path the user specified is correct, if not print an error message.
Is there a way I can validate this in the same page before I submit the form?
I used javascript, but it doesnt seem to work as I expected. Thoughts/Suggestions ?
<script>
function checkfolder()
{
var myObject;
myObject = new ActiveXObject("Scripting.FileSystemObject");
if(!myObject.FolderExists()){
alert("Folder does not exist");
}
}
</script>
<form method=post action="some_file.php">
.
.
.
<input type="submit" name="submit" value="submit" onClick='checkFolder()'>
</form>
You're not going to have much luck with this. PHP can't do this because it operates on the server and has no access to the user's computer. JavaScript will fail because browser prevent access to the file system with JavaScript for security reasons.
You don't have any way of accessing the local data to validate its existence. Even if you do, it's considered really bad.
Instead of writing the file path (which I assume that's what you are doing), just make a javascript Browse file dialog like when you upload an image or file to Gmail. That kind. This ensures that it exists at least when you are trying to upload the file.
Whether the file actually gets deleted after you have selected the file, and before you submit it. It doesn't matter. If it doesn't exist, the upload will fail.