0

In the Create new post section of the page create.php the user has two options:

1) Upload an image using a input[type="file" from its pc.

2) Select an image attached to a meme generator plugin that retrieves the finished canvas image this way....canvas.toDataUrl()

PROBLEM

In the second option, Once the meme is finished, I want to attach it to the input file of the first option to upload it along with the post.

How should I do that?

HTML

  <textarea id="comment"></textarea>
  <input type="file" id="imgtoUpload" />
  <canvas id="meme"></canvas>

JS

  canvas.toDataUrl();// meme to attach

1 Answer 1

1

You don't.

<input type="file"> is specifically for files-from-the-computer, which will be resolved at form post time. If you get data from somewhere else, like a canvas.toDataURL(), then add an <input type="hidden" name="fromcanvas"> and have the second option populate that input with the result of toDataURL. And when the user clicks the file input, wipe the hidden field, and if they use the meme generator, wipe the file input field, so that you never get a post with data in both.

Then when the form is posted, have whatever reads the submitted formdata check for both fields: if there is file input associated with your <input type="file">, use that, and if there's data associated with your hidden input, use that instead.

(and of course, make doubly sure that what you're getting really is image data, and not a .php file. The most common way to backdoor a PHP server is to trick an image upload script into blindly writing a file to disk that was sent as form data in an image input. Validate the input by trying to run it through gd or imagemagick or something before you actually write it to disk!)

Sign up to request clarification or add additional context in comments.

3 Comments

$(“.fromCanvas”).val(canvas.toDataURL()) So...this would be a possible solution?
yes, provided you decode it properly on the PHP side (since file input and canvas data uris are different types of data)
I marked your answer. I followed your directions and got it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.