简体   繁体   English

如何使用CGI Perl在HTML中放置图片?

[英]How to put a picture in HTML using CGI Perl?

I am creating a very simple HTML form. 我正在创建一个非常简单的HTML表单。 I have included the form in the CGI file. 我已经将该表格包含在CGI文件中。 I am using perl scripting. 我正在使用perl脚本。 I have tried to include a picture in my form using the following CGI script: 我尝试使用以下CGI脚本在我的表单中添加图片:

print "<img src=picture1.jpg style=width:600px;height:300px;>";

The problem with this is that the picture in the form does not show, but I don't get any errors and everything else works fine when I access the form from a browser. 问题在于表单中的图片没有显示,但是我没有收到任何错误,并且当我从浏览器访问表单时,其他所有东西都可以正常工作。

Screenshot of the problem 问题的屏幕截图

From looking at your screenshot, I would guess that the image source or location specified as src='picture1.jpg' is incorrect. 通过查看您的屏幕截图,我会猜测指定为src='picture1.jpg'的图像源或位置不正确。

Possible reasons can be: 可能的原因可能是:

  • the file picture1.jpg does not exist on your server 服务器上不存在文件picture1.jpg
  • the file picture1.jpg is in a different directory 文件picture1.jpg在另一个目录中

I bet it's the second problem. 我敢打赌这是第二个问题。 Your CGI script is most likely in a directory called like cgi-bin , so by using src='picture1.jpg' you look for the picture in this cgi-bin directory. 您的CGI脚本很可能位于名为cgi-bin的目录中,因此,使用src='picture1.jpg'可以在此cgi-bin目录中查找图片。

Are you really sure your picture is located here: cgi-bin/picture1.jpg ? 您是否真的确定图片位于: cgi-bin/picture1.jpg Most likely not. 很有可能不会。

I guess the picture is somewhere else, which means you have to set the correct path for it as shown in the examples below: 我猜图片在其他地方,这意味着您必须为其设置正确的路径,如以下示例所示:

img src='../picture1.jpg'

This means "Go one directory level up and look for the picture1.jpg ". 这意味着“向上一级目录并查找picture1.jpg ”。

img src='../images/picture1.jpg'

This means: "Go one directory level up and look for the picture1.jpg inside the image directory". 这意味着:“向上一级目录,在image目录中查找picture1.jpg ”。

img src='/html/images/picture1.jpg'

This means: "Go to the root directory of your website, find the html directory, then find the image directory and finally picture1.jpg inside. 这意味着:“转到您网站的根目录,找到html目录,然后找到image目录,最后picture1.jpg里面的picture1.jpg

After you adjusted the path according to your server's directory structure, the image should show up as desired. 根据服务器的目录结构调整路径后,该图像应显示在所需位置。 Furthermore, as mentioned in the other answers, you should always format your HTML attributes properly. 此外,如其他答案所述,您应始终正确设置HTML属性的格式。

确保为属性使用正确的格式

print "<img src='picture1.jpg' style='width:600px;height:300px;'>";

您需要在html中使用双引号。

print q{<img src="picture1.jpg" width="600" height="300">};

the picture in the form does not show, but I don't get any errors 表格中的图片没有显示,但是我没有任何错误

You don't get any errors in the browser, but have you checked the web server error log? 您在浏览器中没有任何错误,但是您是否检查了Web服务器错误日志?

You don't say where your CGI program and your image are both actually stored on your web server, so it's hard to be sure what the problem is, but I expect you're getting an error in the web server log which would clear things up. 您没有说CGI程序和图像都实际存储在Web服务器上的位置,因此很难确定问题出在哪里,但是我希望Web服务器日志中出现错误,这将清除所有内容起来

If I had to guess (I don't, but I'm going to anyway!) I'd say that the problem is down to the location of the image. 如果我不得不猜测(我没有,但是我还是要去!),我会说问题出在图像的位置。 A common way to set up CGI programs on a web server is for a certain directory (often called cgi-bin ) to be designated as a location to contain all of your CGI programs. 在Web服务器上设置CGI程序的一种常见方法是将某个目录(通常称为cgi-bin )指定为包含所有CGI程序的位置。 But actually, what this means is that you are saying to your web server "everything in this directory is a CGI program - any time you get a request for a resource in this directory, you need to execute the program and return the results to the browser". 但是实际上,这意味着您对Web服务器说:“此目录中的所有内容都是CGI程序-每当您在此目录中请求资源时,都需要执行该程序并将结果返回给浏览器”。

So the web server thinks that everything in that directory is a CGI program. 因此,Web服务器认为该目录中的所有内容都是CGI程序。 This means that you can't put image files in this directory. 这意味着您不能将图像文件放在此目录中。 Because if the web server gets a request for /cgi-bin/picture1.jpg , it will think that picture .jpg is program that returns the image - so it will try to execute the image file. 因为如果Web服务器收到对/cgi-bin/picture1.jpg的请求,它将认为picture .jpg是返回图像的程序-因此它将尝试执行图像文件。 And that's not goitn to work and you'll get a 500 error in your error log. 那不是正常工作,并且您的错误日志中会出现500错误。

The code you have shown us is this: 您显示给我们的代码是这样的:

print "<img src=picture1.jpg style=width:600px;height:300px;>";

Because you have src=picture1.jpg , the web server assumes that the image file is in the current directory - which will be the directory containing the CGI program. 因为您具有src=picture1.jpg ,所以Web服务器假定图像文件位于当前目录中-该目录将是包含CGI程序的目录。 So there's a good chance that this is the problem I described above. 因此,很有可能这就是我上面描述的问题。

The other option is that you have configured your web server so that anything with a certain extension (often .cgi ) should be seen as a CGI program. 另一个选择是您已配置了Web服务器,因此具有特定扩展名(通常是.cgi )的任何内容都应视为CGI程序。 If that's the case, then my previous explanation isn't going to explain your problem. 如果是这样,那么我之前的解释将不会解释您的问题。 In that case, it's most likely that you've just got the URL of the image file wrong and you'll get a 404 error in the web server error log. 在这种情况下,很可能您只是错误地输入了图像文件的URL,并且在Web服务器错误日志中将收到404错误。

But in both cases, there will almost certainly be more useful information waiting for you in the web server error log. 但是在两种情况下,Web服务器错误日志中几乎肯定会有更多有用的信息在等您。 Please look there and add the details to your question. 请查看那里,并将详细信息添加到您的问题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM