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.
Related
For a website, the user should be able to type a title and post in a form and press a submit button to enter them. Upon pressing the submit button, the data in the forms are written to their assigned text files which are in the same folder as the webpage. The writing works fine, but as you can see below, I have attempted to make it so that if the user clicks the submit button without one of the two fields (or neither of the fields) being filled in the site will prompt them to enter text and no file writing takes place.
My issue is that when they click submit and a field isn't filled, this will actually affect the .txt files and they will also end up blank. I want the files to retain their 'old' text unless both textareas in the form contain data to be overwritten. For example, if both .txt files have text in them and I click submit on the form with no text in the form, the two .txt files will keep their text instead of being overwritten to have no text (which is what currently happens).
Here's the html and php:
<form action="AdminBlog.php" method="post">
Blog Title:<textarea type="text" name="titleInput"></textarea><br><br>
Blog Post:<textarea type="text" name="postInput"></textarea><br><br><br>
<input type="submit" value="Submit"/>
</form>
<?php
$titleFile = fopen("Title.txt","w");
$blogTitle = null;
$postFile = fopen("Post.txt","w");
$blogPost = null;
if(!empty($_POST['titleInput']) && !empty($_POST['postInput'])) {
$blogTitle = $_POST['titleInput'];
fwrite($titleFile,$blogTitle);
fclose($titleFile);
$blogPost = $_POST['postInput'];
fwrite($postFile,$blogPost);
fclose($postFile);
}
if (empty($_POST['postInput']) || empty($_POST['titleInput'])) {
echo "Please enter a title and post.";
}
?>
Changing that final if statement to elseif or else didn't seem to make a difference. The echo is outputted when it should be, but the .txt files are still overwritten with no data (when a textarea is blank and user clicks submit).
EDIT: Just wanted to add something. I'm not sure if this is relevant, but whether there is text in the files or not, I cannot read the text from another .php file. I can from this same one, but when trying to get the .txt file contents in another .php page, I cannot do so. This is also causing me a separate problem, just wanted to mention in case it was anything useful.
I think you need to try
file_put_contents($file, $person, FILE_APPEND);
for write file.
1st argument will be name & path of the file.
2nd argument will be data in string format.
3rd argument will be FILE_APPEND if you want to add content in existing file.
if you will not use 3nd argument then it will replace content of the file all time.
Source
There is a > missing in your html after <textarea type="text" name="postInput".
Try to put that on.
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 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.
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
Been scratching my head for too long on this: Using jquery.form, (http://malsup.com/jquery/form) with PHP ... my $_FILES['someimage'] gets set but the error number is always UPLOAD_ERR_NO_FILE, size is also 0.
The JavaScript:
$('form input[type=file]').change(function () {
$(this).clone().appendTo('#imgform');
$('#imgform').ajaxForm();
$('#imgform').ajaxSubmit({
type: 'POST'
});
});
Which appends to:
<form id="imgform" method="POST" action="/api/images.php" enctype="multipart/form-data"></form>
From another form which has bog-standard file inputs.
PHP logs are clean, but var_dumping $_FILES always shows that the index is set to the name of the form element ... but no data.
Thanks guys!
(Sorry, I know jQuery-like questions are too frequent round these parts).
EDIT
I found Clone a file input element in Javascript which contains further information and suggested alternatives.
What I decided to do is have a single form for non JavaScript browsers, and JavaScript/jQuery breaks the single form into three forms:
Head form -> File upload form -> tail form
Then I can post the file upload async, and when the tail's submit is clicked, glue the form together into a POST as they are just text fields.
Two things I see when I try to run this. Since you are cloning then appending, I wonder if your file input exists within the context of the form. If not, then $('form input[type=file]') will never find the element to be cloned.
Perhaps the biggest problem, though, is in the way browsers handle file upload controls. You cannot programmatically set a value on a file input control - otherwise it would be trivial as a web developer to automatically set the file upload value to "c:\Files\MyPasswordFile.txt" and automatically submit the form invisibly to the user.
When I changed your code to this:
<input type="file" name="imageFile" />
<form id="imgform" method="POST" action="/api/images.php" enctype="multipart/form-data">
</form>
<script>
$('input[type=file]').change(function() {
alert("ACTION");
$(this).clone().appendTo('#imgform');
//$('#imgform').ajaxForm();
//$('#imgform').ajaxSubmit(
// {
// type: 'POST'
// }
// );
});
</script>
I can see the behavior as above - the field is cloned and appended - but it has no value. Since part of the clone process involves setting the field value - this would violate that security restriction and thus fails.
You can't post files using ajax as javascript cannot access any local hard drive for security reasons.
There are ways to mimic ajax posting using iFrames. This link is a good example.
http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html