I am trying to get an image from the user by using file input method. It is successfully getting the image from the user. But I want this input to be passed to my next line of code.
我试图通过使用文件输入方法从用户获取图像。它成功地从用户获取图像。但我希望将此输入传递给我的下一行代码。
Any help would be appreciated.
任何帮助,将不胜感激。
<input type='file' name='image'>
$two=createfrompng("Here i want this input to be passed");
$ two = createfrompng(“我想要传递此输入”);
3 个解决方案
#1
You can use $_FILES
global variable. Example :
您可以使用$ _FILES全局变量。示例:
var_dump($_FILES['image']);
#2
You can't. Your PHP code is executed server-side. Before the resulting html is send to the client so you don't know if the user WILL upload a file or not and even less it's contents.
你不能。您的PHP代码在服务器端执行。在生成的html发送到客户端之前,您不知道用户是否会上传文件,甚至更少的内容。
The effect you are trying to achieve should be done client-side by altering the DOM with javascript.
您尝试实现的效果应该通过使用javascript更改DOM来在客户端完成。
But anyway you can't immediately access the image because, for security reasons, javascript can't access client's filesystem so you need to upload the file (the way you are doing) process it server-side and send back to the client, which is difficult if you haven't a websocket connection to start comunication to the client.
但无论如何你不能立即访问图像,因为出于安全原因,javascript无法访问客户端的文件系统,所以你需要上传文件(你正在做的方式)处理服务器端并发送回客户端,如果您没有websocket连接来启动与客户端的通信,那将很困难。
I suggest you to slightly change your approach and submit your form targetted to an existing container in your page (tipically a div) and respond with only the html to render the image inside it.
我建议你稍微改变一下你的方法,然后将你的表单提交到页面中的一个现有容器(一个div),并只用html来回应它里面的图像。
See the jQuery .load() function. It is a simple solution and I think it will be the best for you.
请参阅jQuery .load()函数。这是一个简单的解决方案,我认为这对您来说是最好的。
…or another even simpler solution is to reload the whole page and mantain state data server side (if you want to manage multiple images as I thought to understand). It is less responsive. But not to much expensive (if your html is not huge) because the browser cache will remember your images (if you does'nt change the url to download them).
...或者另一个更简单的解决方案是重新加载整个页面并保持状态数据服务器端(如果你想管理我认为理解的多个图像)。响应性较差。但不要太贵(如果你的html不是很大),因为浏览器缓存会记住你的图像(如果你没有更改网址下载它们)。
#3
Check first, if the file exists in $_FILES
and the use it, in your case:
首先检查,如果文件存在于$ _FILES中并且使用它,在您的情况下:
$image = getimagesize($_FILES['image']['tmp_name']);
if ($image !== false) {
$two = createfrompng($image);
}
#1
You can use $_FILES
global variable. Example :
您可以使用$ _FILES全局变量。示例:
var_dump($_FILES['image']);
#2
You can't. Your PHP code is executed server-side. Before the resulting html is send to the client so you don't know if the user WILL upload a file or not and even less it's contents.
你不能。您的PHP代码在服务器端执行。在生成的html发送到客户端之前,您不知道用户是否会上传文件,甚至更少的内容。
The effect you are trying to achieve should be done client-side by altering the DOM with javascript.
您尝试实现的效果应该通过使用javascript更改DOM来在客户端完成。
But anyway you can't immediately access the image because, for security reasons, javascript can't access client's filesystem so you need to upload the file (the way you are doing) process it server-side and send back to the client, which is difficult if you haven't a websocket connection to start comunication to the client.
但无论如何你不能立即访问图像,因为出于安全原因,javascript无法访问客户端的文件系统,所以你需要上传文件(你正在做的方式)处理服务器端并发送回客户端,如果您没有websocket连接来启动与客户端的通信,那将很困难。
I suggest you to slightly change your approach and submit your form targetted to an existing container in your page (tipically a div) and respond with only the html to render the image inside it.
我建议你稍微改变一下你的方法,然后将你的表单提交到页面中的一个现有容器(一个div),并只用html来回应它里面的图像。
See the jQuery .load() function. It is a simple solution and I think it will be the best for you.
请参阅jQuery .load()函数。这是一个简单的解决方案,我认为这对您来说是最好的。
…or another even simpler solution is to reload the whole page and mantain state data server side (if you want to manage multiple images as I thought to understand). It is less responsive. But not to much expensive (if your html is not huge) because the browser cache will remember your images (if you does'nt change the url to download them).
...或者另一个更简单的解决方案是重新加载整个页面并保持状态数据服务器端(如果你想管理我认为理解的多个图像)。响应性较差。但不要太贵(如果你的html不是很大),因为浏览器缓存会记住你的图像(如果你没有更改网址下载它们)。
#3
Check first, if the file exists in $_FILES
and the use it, in your case:
首先检查,如果文件存在于$ _FILES中并且使用它,在您的情况下:
$image = getimagesize($_FILES['image']['tmp_name']);
if ($image !== false) {
$two = createfrompng($image);
}