I was trying to learn a bit of web development. I want to create a website where I can upload a image and convert it to pdf. For this I added a form tag in HTML:
//To create a button where I can upload an image document
<form action="/php/upload.php" method="post" enctype="multipart/form-data">
<p>Upload</p>
<input type="file" id="fileToUpload" name="fileToUpload">
<input type="submit" value="submit" name="submit">
</form>
// To create a button where I can convert the **uploaded** image document
<form action="/php/converter.php ../uploads/700kb.jpg" method="post">
<p>Convert</p>
<input type="submit">
</form>
In the first script, "upload.php", I end up having the image saved/uploaded on the server (say the location of file on the server is "../uploads/700kb.jpg"). This is fine.
But now I want to run the other script which converts image to pdf when I press the other submit button which calls "converter.php". The issue now is, I somehow need to pass an argument to "converter.php" about the location of the uploaded file. I tried finding on web but I didn't get what I was looking for. So what I wished to do is to somehow save the returned
the target file path in a variable in HTML when the "upload.php" script is called and then pass this variable as an argument to the other PHP script as converter.php <target_file_path>
. (This is what I read on google that it should be possible)
To test if at least this way of passing an argument to PHP script works, I manually tried to add the target file path in HTML form tag, <form action="/php/converter.php ../uploads/700kb.jpg" method="post">
. However, it didn't work (ofcourse the target file path exists). So this way of passing an argument to a PHP script in a form tag of HTML does not work? Or this works only in command line like php php/converter.php ../uploads/700kb.jpg
?
Moreover, my first problem still remains unsolved. How could I save the return
(i.e. target file path from the upload.php) as a variable (or something similar in html) and then pass it to the other script converter.php
when the corresponding submit button is pressed.
Also, I do not want to merge the two php scripts into one if possible.
Thank you very much for the help!
If you're always using the same file location and name, the simplest way to pass it to the next script is to add it to a query string in the URL to which the Convert button or link goes (that's the part of a URL you see after a question mark sometimes), and then convert.php can read it from $_GET. If you want to continue using the form, add a hidden input to the form with the path to the file as the value, and have convert.php read it from $_GET if the form method is get or from $_POST if the form method is post. There are many online tutorials about these if you're not familiar with them.
It might be better to add a session variable in which to temporarily store the uploaded file path, if there's any chance anyone might want to try to mess around with your uploaded files; that makes it harder to find out anything about how the file is being handled on the back end. Your convert script can then just read it from the session. There are also many good articles and tutorials explaining the PHP session object.