如何在modx革命中上传图像而不重新加载页面

时间:2012-01-11 19:49:41

标签: image modx modx-revolution

如何在网络环境(前端)中上传图片而无需在modx革命中重新加载页面?我正在尝试使用fileupload额外上传图片,但它正在重新加载页面。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

最后我得到了答案。我们可以通过使用简单的PHP代码和一个小的JavaScript来不使用FILEUPLOAD插件来实现它。 php代码: -

<?php

$path = "uploads/";

$valid_formats = array("jpg", "png", "gif", "bmp","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024)) // Image size max 1 MB
{
$actual_image_name = time().".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{

echo "image uploaded";
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
}
else
echo "Please select image..!";
exit;
}

其他javascript和你可以参考以下链接: - http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html